htiui/HtiServicePlugins/HtiPIMServicePlugin/src/PIMHandler.cpp
branchRCL_3
changeset 11 454d022d514b
parent 0 d6fe6244b863
equal deleted inserted replaced
9:404ad6c9bc20 11:454d022d514b
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 // INCLUDE FILES
    19 // INCLUDE FILES
       
    20 #include "../../../symbian_version.hrh"
       
    21 
    20 #include "HtiPIMServicePlugin.h"
    22 #include "HtiPIMServicePlugin.h"
    21 #include "PIMHandler.h"
    23 #include "PIMHandler.h"
    22 
    24 
    23 #include <HtiDispatcherInterface.h>
    25 #include <HtiDispatcherInterface.h>
    24 #include <HTILogging.h>
    26 #include <HtiLogging.h>
    25 
    27 
    26 #include <utf.h>
    28 #include <utf.h>
    27 #include <calcommon.h>
    29 #include <calcommon.h>
    28 #include <calsession.h>
    30 #include <calsession.h>
    29 #include <calenimporter.h>
       
    30 #include <calentryview.h>
    31 #include <calentryview.h>
    31 
    32 
    32 #include <CVPbkContactLinkArray.h>
    33 #include <cntdb.h> 
    33 #include <CVPbkContactManager.h>
    34 #include <cntitem.h> 
    34 #include <CVPbkContactViewDefinition.h>
    35 #include <cntfldst.h> 
    35 #include <CVPbkContactStoreUriArray.h>
    36 #include <cntvcard.h> 
    36 #include <CVPbkSortOrder.h>
    37 #include <cntfilt.h> 
    37 #include <CVPbkVCardEng.h>
    38 #include <caldataexchange.h> 
    38 #include <MVPbkContactLinkArray.h>
    39 #include <caldataformat.h> 
    39 #include <MVPbkContactOperationBase.h>
       
    40 #include <MVPbkContactStore.h>
       
    41 #include <MVPbkContactStoreList.h>
       
    42 #include <MVPbkContactStoreObserver.h>
       
    43 #include <MVPbkContactStoreProperties.h>
       
    44 #include <MVPbkContactView.h>
       
    45 #include <MVPbkContactViewBase.h>
       
    46 #include <MVPbkViewContact.h>
       
    47 #include <TVPbkContactStoreUriPtr.h>
       
    48 #include <VPbkContactStoreUris.h>
       
    49 
    40 
    50 // CONSTANTS
    41 // CONSTANTS
    51 _LIT8( KErrorUnrecognizedCommand, "Unrecognized command" );
    42 _LIT8( KErrorUnrecognizedCommand, "Unrecognized command" );
       
    43 _LIT8( KErrorCalendarFileFormat, "Invalid format of calendar file name");
    52 _LIT8( KErrorVCardImportFailed, "vCard import failed" );
    44 _LIT8( KErrorVCardImportFailed, "vCard import failed" );
    53 _LIT8( KErrorVCalendarImportFailed, "vCalendar import failed" );
    45 _LIT8( KErrorVCalendarImportFailed, "vCalendar import failed" );
    54 _LIT8( KErrorMissingVCalendar, "Missing vCalendar object" );
    46 _LIT8( KErrorMissingVCalendar, "Missing vCalendar object" );
    55 _LIT8( KErrorMissingVCard, "Missing vCard object" );
    47 _LIT8( KErrorMissingVCard, "Missing vCard object" );
    56 _LIT8( KErrorInvalidId, "Invalid ID parameter" );
    48 _LIT8( KErrorInvalidId, "Invalid ID parameter" );
    57 _LIT8( KErrorItemNotFound, "Item not found" );
    49 _LIT8( KErrorItemNotFound, "Item not found" );
    58 _LIT8( KErrorFailedDelete, "Failed to delete item" );
    50 _LIT8( KErrorFailedDelete, "Failed to delete item" );
    59 _LIT8( KErrorFailedDeleteAll, "Failed to delete all items" );
    51 _LIT8( KErrorFailedDeleteAll, "Failed to delete all items" );
    60 _LIT8( KErrorIdDeleteNotSupported, "Deleting with ID not supported anymore" );
    52 _LIT8( KErrorFailedOpenCalendar, "Failed to open calendar file");
    61 
    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 )  
    62 _LIT8( KErrorMissingText, "Text parameter missing" );
    60 _LIT8( KErrorMissingText, "Text parameter missing" );
    63 _LIT8( KErrorMissingFilepath, "Filepath parameter missing" );
    61 _LIT8( KErrorMissingFilepath, "Filepath parameter missing" );
    64 _LIT8( KErrorNotepadAddMemoFailed, "Notepad add memo failed" );
    62 _LIT8( KErrorNotepadAddMemoFailed, "Notepad add memo failed" );
    65 _LIT8( KErrorNotepadAddMemoFromFileFailed, "Notepad add memo from file failed" );
    63 _LIT8( KErrorNotepadAddMemoFromFileFailed, "Notepad add memo from file failed" );
    66 _LIT8( KErrorNotepadDeleteAllFailed, "Notepad delete all failed" );
    64 _LIT8( KErrorNotepadDeleteAllFailed, "Notepad delete all failed" );
    68 _LIT( KHtiNpdHlpExe,       "HtiNpdHlp.exe" );
    66 _LIT( KHtiNpdHlpExe,       "HtiNpdHlp.exe" );
    69 _LIT( KCmdAddMemo,         "AddMemo" );
    67 _LIT( KCmdAddMemo,         "AddMemo" );
    70 _LIT( KCmdAddMemoFromFile, "AddMemoFromFile" );
    68 _LIT( KCmdAddMemoFromFile, "AddMemoFromFile" );
    71 _LIT( KCmdDeleteAll,       "DeleteAll" );
    69 _LIT( KCmdDeleteAll,       "DeleteAll" );
    72 _LIT( KCmdDelim,           " " );
    70 _LIT( KCmdDelim,           " " );
    73 
    71 #else
    74 _LIT( KDefaultAgendaFile, "" ); // A default file is opened if fileName is KNullDesC
    72 _LIT8( KErrorNotepadNotSupported, "Notepad not supported" );
    75 
    73 #endif
    76 
    74 
    77 // ----------------------------------------------------------------------------
    75 // ----------------------------------------------------------------------------
    78 CPIMHandler* CPIMHandler::NewL()
    76 CPIMHandler* CPIMHandler::NewL()
    79     {
    77     {
    80     HTI_LOG_FUNC_IN( "CPIMHandler::NewL" );
    78     HTI_LOG_FUNC_IN( "CPIMHandler::NewL" );
    85     HTI_LOG_FUNC_OUT( "CPIMHandler::Done" );
    83     HTI_LOG_FUNC_OUT( "CPIMHandler::Done" );
    86     return self;
    84     return self;
    87     }
    85     }
    88 
    86 
    89 // ----------------------------------------------------------------------------
    87 // ----------------------------------------------------------------------------
    90 CPIMHandler::CPIMHandler():iIsBusy( EFalse ), iEntryViewErr( KErrNone )
    88 CPIMHandler::CPIMHandler():iIsBusy( EFalse ), iEntryViewErr( KErrNone ),
       
    89         iCalSession(NULL)
    91     {
    90     {
    92     }
    91     }
    93 
    92 
    94 // ----------------------------------------------------------------------------
    93 // ----------------------------------------------------------------------------
    95 CPIMHandler::~CPIMHandler()
    94 CPIMHandler::~CPIMHandler()
    96     {
    95     {
    97     HTI_LOG_TEXT( "CPIMHandler destroy" );
    96     HTI_LOG_TEXT( "CPIMHandler destroy" );
    98     HTI_LOG_TEXT( "Deleting iEntryView" );
    97     HTI_LOG_TEXT( "Deleting iCalSession");
    99     delete iEntryView;
       
   100     HTI_LOG_TEXT( "Deleting iCalSession" );
       
   101     delete iCalSession;
    98     delete iCalSession;
   102     if ( iContactView )
       
   103         {
       
   104         HTI_LOG_TEXT( "Deleting iContactView" );
       
   105         iContactView->RemoveObserver( *this );
       
   106         delete iContactView;
       
   107         }
       
   108     HTI_LOG_TEXT( "Deleting iVCardEngine" );
       
   109     delete iVCardEngine;
       
   110     HTI_LOG_TEXT( "Deleting iContactManager" );
       
   111     delete iContactManager;
       
   112     HTI_LOG_TEXT( "Deleting iBuffer" );
       
   113     delete iBuffer;
       
   114     HTI_LOG_TEXT( "Deleting iWaiter" );
    99     HTI_LOG_TEXT( "Deleting iWaiter" );
   115     delete iWaiter;
   100     delete iWaiter;
   116     }
   101     }
   117 
   102 
   118 // ----------------------------------------------------------------------------
   103 // ----------------------------------------------------------------------------
   119 void CPIMHandler::ConstructL()
   104 void CPIMHandler::ConstructL()
   120     {
   105     {
   121     HTI_LOG_TEXT( "CPIMHandler::ConstructL" );
   106     HTI_LOG_TEXT( "CPIMHandler::ConstructL" );
   122     iWaiter = new ( ELeave ) CActiveSchedulerWait;
   107     iWaiter = new ( ELeave ) CActiveSchedulerWait;
   123 
       
   124     }
   108     }
   125 
   109 
   126 // ----------------------------------------------------------------------------
   110 // ----------------------------------------------------------------------------
   127 void CPIMHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
   111 void CPIMHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
   128     {
   112     {
   139     TInt err = KErrNone;
   123     TInt err = KErrNone;
   140 
   124 
   141     // Zero legth of aMessage tested already in CHtiPIMServicePlugin.
   125     // Zero legth of aMessage tested already in CHtiPIMServicePlugin.
   142     // Other sanity checks must be done here.
   126     // Other sanity checks must be done here.
   143 
   127 
   144     iCommand = aMessage.Ptr()[0];
   128     TUint8 command = aMessage.Ptr()[0];
   145     switch ( iCommand )
   129     switch ( command )
   146         {
   130         {
   147         case CHtiPIMServicePlugin::EImportVCard:
   131         case CHtiPIMServicePlugin::EImportVCard:
   148             {
   132             {
   149             TRAP( err, HandleVCardImportFuncL(
   133             TRAP( err, HandleVCardImportFuncL(
   150                     aMessage.Right( aMessage.Length() - 1 ) ) );
   134                     aMessage.Right( aMessage.Length() - 1 ) ) );
   166             {
   150             {
   167             TRAP( err, HandleCalendarDeleteFuncL(
   151             TRAP( err, HandleCalendarDeleteFuncL(
   168                     aMessage.Right( aMessage.Length() - 1 ) ) );
   152                     aMessage.Right( aMessage.Length() - 1 ) ) );
   169             break;
   153             break;
   170             }
   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             }
   171         case CHtiPIMServicePlugin::ENotepadAddMemo:
   166         case CHtiPIMServicePlugin::ENotepadAddMemo:
   172             {
   167             {
   173             TRAP( err, HandleNotepadAddMemoFuncL( aMessage.Mid( 1 ) ) );
   168             TRAP( err, HandleNotepadAddMemoFuncL( aMessage.Mid( 1 ) ) );
   174             break;
   169             break;
   175             }
   170             }
   205     {
   200     {
   206     return iIsBusy;
   201     return iIsBusy;
   207     }
   202     }
   208 
   203 
   209 // ----------------------------------------------------------------------------
   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 // ----------------------------------------------------------------------------
   210 void CPIMHandler::HandleVCardImportFuncL( const TDesC8& aData )
   297 void CPIMHandler::HandleVCardImportFuncL( const TDesC8& aData )
   211     {
   298     {
   212     HTI_LOG_FUNC_IN( "CPIMHandler::HandleVCardImportFuncL" );
   299     HTI_LOG_FUNC_IN( "CPIMHandler::HandleVCardImportFuncL" );
   213 
       
   214     if ( aData.Length() == 0 )
   300     if ( aData.Length() == 0 )
   215         {
   301         {
   216         SendErrorMessageL( KErrArgument, KErrorMissingVCard );
   302         SendErrorMessageL( KErrArgument, KErrorMissingVCard );
   217         return;
   303         return;
   218         }
   304         }
   219 
   305     
   220     if ( iBuffer ) // delete if exists (just to be sure)
   306     CContactDatabase* contactDatabase = CContactDatabase::OpenL();
   221         {
   307     HTI_LOG_TEXT( "CPIMHandler: Contact database open" );
   222         delete iBuffer;
   308     CleanupStack::PushL(contactDatabase);
   223         iBuffer = NULL;
   309     
   224         }
   310     CBufFlat* buffer = CBufFlat::NewL(aData.Length());
   225     iBuffer = CBufFlat::NewL( aData.Length() );
   311     CleanupStack::PushL(buffer);
   226     iBuffer->ExpandL( 0, aData.Length() );
   312     buffer->ExpandL(0, aData.Length());
   227 
   313     buffer->Ptr(0).Copy(aData.Right(aData.Length()));
   228     HTI_LOG_FORMAT( "Data length = %d", aData.Length() );
   314     RBufReadStream readStream;
   229     HTI_LOG_FORMAT( "Buffer length = %d", iBuffer->Ptr( 0 ).MaxLength() );
   315     readStream.Open(*buffer, 0);
   230     iBuffer->Ptr( 0 ).Copy( aData );
   316     CleanupClosePushL<RBufReadStream>( readStream );
   231 
   317     
   232     if ( iContactManager == NULL )
   318     // Imports vCard
   233         {
   319     TBool success = EFalse;
   234         CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewLC();
   320     TUid format = TUid::Uid(KUidVCardConvDefaultImpl);
   235         uriArray->AppendL( TVPbkContactStoreUriPtr(
   321     CArrayPtr<CContactItem>* contacts = NULL;
   236             VPbkContactStoreUris::DefaultCntDbUri() ) );
   322     TRAPD(err,contacts = contactDatabase->ImportContactsL(format, readStream, success, 
   237         iContactManager = CVPbkContactManager::NewL( *uriArray );
   323             CContactDatabase::EImportSingleContact | CContactDatabase::ETTFormat));
   238         CleanupStack::PopAndDestroy( uriArray );
   324     CleanupStack::PushL(contacts);
   239         }
   325     if(err != KErrNone || success == EFalse || contacts == NULL || contacts->Count() == 0)
   240 
   326         {
   241     if ( iVCardEngine == NULL )
   327         HTI_LOG_TEXT("Failed to import vCard");
   242         {
   328         SendErrorMessageL( err, KErrorVCardImportFailed );
   243         iVCardEngine = CVPbkVCardEng::NewL( *iContactManager );
   329         }
   244         }
   330     else
   245 
   331         {
   246     MVPbkContactStoreList& stores = iContactManager->ContactStoresL();
   332         // Returns the imported contact id 
   247     iContactStore = &stores.At( 0 );
   333         TInt32 entryId = contacts->At(0)->Id();
   248     iContactStore->OpenL( *this );
   334         TBuf8<4> idBuf;
   249 
   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
   250     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCardImportFuncL: Done" );
   343     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCardImportFuncL: Done" );
   251     }
   344     }
   252 
   345 
   253 // ----------------------------------------------------------------------------
   346 // ----------------------------------------------------------------------------
   254 void CPIMHandler::HandleVCalendarImportFuncL( const TDesC8& aData )
   347 void CPIMHandler::HandleVCalendarImportFuncL( const TDesC8& aData )
   260         HTI_LOG_TEXT( "CPIMHandler::HandleVCalendarImportFuncL: Error: length of data is zero" )
   353         HTI_LOG_TEXT( "CPIMHandler::HandleVCalendarImportFuncL: Error: length of data is zero" )
   261         SendErrorMessageL( KErrArgument, KErrorMissingVCalendar );
   354         SendErrorMessageL( KErrArgument, KErrorMissingVCalendar );
   262         return;
   355         return;
   263         }
   356         }
   264 
   357 
   265     if ( iBuffer ) // delete if exists (just to be sure)
   358     CBufFlat* buffer = CBufFlat::NewL( aData.Length() );
   266         {
   359     CleanupStack::PushL(buffer);
   267         delete iBuffer;
   360     buffer->ExpandL( 0, aData.Length() );
   268         iBuffer = NULL;
   361     buffer->Ptr( 0 ).Copy( aData );
   269         }
       
   270     iBuffer = CBufFlat::NewL( aData.Length() );
       
   271     iBuffer->ExpandL( 0, aData.Length() );
       
   272     iBuffer->Ptr( 0 ).Copy( aData );
       
   273     RBufReadStream readStream;
   362     RBufReadStream readStream;
   274     readStream.Open( *iBuffer, 0 );
   363     readStream.Open( *buffer, 0 );
   275     CleanupClosePushL( readStream );
   364     CleanupClosePushL( readStream );
   276 
   365 
   277     if ( iCalSession == NULL )
   366     if(iCalSession == NULL)
   278         {
   367         {
   279         HTI_LOG_TEXT( "CPIMHandler: Creating Calendar session" );
   368         HTI_LOG_TEXT( "CPIMHandler: Open default calendar file" );
   280         iCalSession = CCalSession::NewL();
   369         iCalSession = CCalSession::NewL();
   281         iCalSession->OpenL( KDefaultAgendaFile );
   370         iCalSession->OpenL( KDefaultAgendaFile );
   282         HTI_LOG_TEXT( "CPIMHandler: Calendar session open" );
   371         HTI_LOG_TEXT( "CPIMHandler: Calendar session open" );
   283         }
   372         }
   284 
   373 
   285     CCalenImporter* importer = CCalenImporter::NewL( *iCalSession );
   374     CCalDataExchange* importer = CCalDataExchange::NewL(*iCalSession);
   286     CleanupStack::PushL( importer );
   375     CleanupStack::PushL( importer );
   287     HTI_LOG_TEXT( "CPIMHandler: Calendar importer created" );
   376     HTI_LOG_TEXT( "CPIMHandler: Calendar importer created" );
   288 
   377 
   289     RPointerArray<CCalEntry> entryArray;
   378     RPointerArray<CCalEntry> entryArray;
   290     CleanupClosePushL( entryArray );
   379     CleanupClosePushL( entryArray );
   291 
   380 
   292     TInt err = KErrNone;
   381     TInt err = KErrNone;
   293     TInt size = 0;
   382     TInt size = 0;
   294     importer->SetImportMode( ECalenImportModeExtended );
   383     // Import as VCalendar
   295     // First try to import as iCalendar
   384     TRAP( err, importer->ImportL( KUidVCalendar, readStream, entryArray ) );
   296     TRAP( err, importer->ImportICalendarL( readStream, entryArray ) );
   385     HTI_LOG_FORMAT( "ImportL return value %d", err );
   297     HTI_LOG_FORMAT( "ImportICalendarL return value %d", err );
       
   298     size = entryArray.Count();
   386     size = entryArray.Count();
   299     HTI_LOG_FORMAT( "Import ICalendarL imported %d entries", size );
   387     HTI_LOG_FORMAT( "Import VCalendarL imported %d entries", size );
   300     // If import didn't succeed, try as vCalendar
       
   301     if ( err != KErrNone || size == 0 )
       
   302         {
       
   303         readStream.Close();
       
   304         readStream.Open( *iBuffer, 0 ); // reset read stream
       
   305         entryArray.ResetAndDestroy(); // avoid double imports
       
   306         TRAP( err, importer->ImportVCalendarL( readStream, entryArray ) );
       
   307         HTI_LOG_FORMAT( "ImportVCalendarL return value %d", err );
       
   308         size = entryArray.Count();
       
   309         HTI_LOG_FORMAT( "Import VCalendarL imported %d entries", size );
       
   310         }
       
   311     TCalLocalUid uniqueId = 0;
   388     TCalLocalUid uniqueId = 0;
   312     TInt success = 0;
   389     TInt success = 0;
   313     if ( size > 0 )
   390     if ( size > 0 )
   314         {
   391         {
   315         iEntryView = CCalEntryView::NewL( *iCalSession, *this );
   392         CCalEntryView* entryView = CCalEntryView::NewL( *iCalSession, *this );
   316         iWaiter->Start();
   393         iWaiter->Start();
       
   394         CleanupStack::PushL(entryView);
   317         if ( iEntryViewErr == KErrNone )
   395         if ( iEntryViewErr == KErrNone )
   318             {
   396             {
   319             TRAP( err, iEntryView->StoreL( entryArray, success ) );
   397             TRAP( err, entryView->StoreL( entryArray, success ) );
   320             HTI_LOG_FORMAT( "StoreL return value %d", err );
   398             HTI_LOG_FORMAT( "StoreL return value %d", err );
   321             HTI_LOG_FORMAT( "Successfully stored %d entries", success );
   399             HTI_LOG_FORMAT( "Successfully stored %d entries", success );
   322             uniqueId = entryArray[0]->LocalUidL();
   400             uniqueId = entryArray[0]->LocalUidL();
   323             }
   401             }
   324         delete iEntryView;
   402         CleanupStack::PopAndDestroy();
   325         iEntryView = NULL;
       
   326         }
   403         }
   327     entryArray.ResetAndDestroy();
   404     entryArray.ResetAndDestroy();
   328     CleanupStack::PopAndDestroy(); // entryArray
   405     CleanupStack::PopAndDestroy(); // entryArray
   329 
   406 
   330     if ( err == KErrNone && success > 0 )
   407     if ( err == KErrNone && success > 0 )
   331         {
   408         {
       
   409         HTI_LOG_TEXT("vCalendar imported");
   332         TBuf8<8> uniqueIdStr;
   410         TBuf8<8> uniqueIdStr;
   333         uniqueIdStr.Copy( ( TUint8* ) ( &uniqueId ), sizeof( uniqueId ) );
   411         uniqueIdStr.Copy( ( TUint8* ) ( &uniqueId ), sizeof( uniqueId ) );
   334         SendOkMsgL( uniqueIdStr );
   412         SendOkMsgL( uniqueIdStr );
   335         }
   413         }
   336     else
   414     else
   337         {
   415         {
   338         if ( err == KErrNone ) err = KErrGeneral;
   416         HTI_LOG_TEXT("Failed to import vCalendar");
       
   417         if ( err == KErrNone ) 
       
   418             {
       
   419             err = KErrGeneral;
       
   420             }
   339         SendErrorMessageL( err, KErrorVCalendarImportFailed );
   421         SendErrorMessageL( err, KErrorVCalendarImportFailed );
   340         }
   422         }
   341 
   423 
   342     CleanupStack::PopAndDestroy( 2 ); // readStream, importer
   424     CleanupStack::PopAndDestroy( 3 ); // buffer, readStream, importer
   343     delete iCalSession;
       
   344     iCalSession = NULL;
       
   345     delete iBuffer;
       
   346     iBuffer = NULL;
       
   347     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCalendarImportFuncL: Done" );
   425     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCalendarImportFuncL: Done" );
   348     }
   426     }
   349 
   427 
   350 // ----------------------------------------------------------------------------
   428 // ----------------------------------------------------------------------------
   351 void CPIMHandler::HandleContactDeleteFuncL( const TDesC8& aData )
   429 void CPIMHandler::HandleContactDeleteFuncL( const TDesC8& aData )
   352     {
   430     {
   353     HTI_LOG_FUNC_IN( "CPIMHandler::HandleContactDeleteFuncL" );
   431     HTI_LOG_FUNC_IN( "CPIMHandler::HandleContactDeleteFuncL" );
   354 
       
   355     TInt dataLength = aData.Length();
   432     TInt dataLength = aData.Length();
   356     if ( dataLength != 0 && dataLength != 4 )
   433     if ( dataLength != 0 && dataLength != 4 )
   357         {
   434         {
   358         HTI_LOG_TEXT( "CPIMHandler: Error: wrong length of data" )
   435         HTI_LOG_TEXT( "CPIMHandler: Error: wrong length of data" )
   359         SendErrorMessageL( KErrArgument, KErrorInvalidId );
   436         SendErrorMessageL( KErrArgument, KErrorInvalidId );
   360         return;
   437         return;
   361         }
   438         }
   362 
   439     CContactDatabase* contactDatabase = CContactDatabase::OpenL();
   363     if ( dataLength == 4 )
   440     HTI_LOG_TEXT( "CPIMHandler: Contact database open" );
   364         {
   441     CleanupStack::PushL(contactDatabase);
   365         SendErrorMessageL( KErrNotSupported, KErrorIdDeleteNotSupported );
   442     
   366         return;
   443     if(dataLength == 0)   // delete all contacts
   367         }
   444         {
   368 
   445         //const CContactIdArray* array = iContactDatabase->SortedItemsL();
   369     if ( iContactManager == NULL )
   446         CCntFilter *filter = CCntFilter::NewLC();
   370         {
   447         filter->SetContactFilterTypeCard(ETrue);
   371         CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewLC();
   448         filter->SetContactFilterTypeGroup(EFalse);
   372         uriArray->AppendL( TVPbkContactStoreUriPtr(
   449         contactDatabase->FilterDatabaseL(*filter);
   373             VPbkContactStoreUris::DefaultCntDbUri() ) );
   450         TRAPD(err, contactDatabase->DeleteContactsL(*filter->iIds));
   374         iContactManager = CVPbkContactManager::NewL( *uriArray );
   451         CleanupStack::PopAndDestroy();
   375         CleanupStack::PopAndDestroy( uriArray );
   452         if(err == KErrNone)
   376         }
   453             {
   377 
   454             HTI_LOG_TEXT("All contacts deleted");
   378     if ( iContactStore == NULL )
   455             SendOkMsgL( KNullDesC8 );
   379         {
   456             }
   380         MVPbkContactStoreList& stores = iContactManager->ContactStoresL();
   457         else
   381         iContactStore = &stores.At( 0 );
   458             {
   382         }
   459             HTI_LOG_TEXT("Failed to delete all contacts");
   383 
   460             SendErrorMessageL( err, KErrorFailedDeleteAll );
   384     iContactStore->OpenL( *this );
   461             }
   385 
   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();
   386     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleContactDeleteFuncL" );
   483     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleContactDeleteFuncL" );
   387     }
   484     }
   388 
   485 
   389 // ----------------------------------------------------------------------------
   486 // ----------------------------------------------------------------------------
   390 void CPIMHandler::HandleCalendarDeleteFuncL( const TDesC8& aData )
   487 void CPIMHandler::HandleCalendarDeleteFuncL( const TDesC8& aData )
   397         HTI_LOG_TEXT( "CPIMHandler: Error: wrong length of data" )
   494         HTI_LOG_TEXT( "CPIMHandler: Error: wrong length of data" )
   398         SendErrorMessageL( KErrArgument, KErrorInvalidId );
   495         SendErrorMessageL( KErrArgument, KErrorInvalidId );
   399         return;
   496         return;
   400         }
   497         }
   401 
   498 
   402     if ( iBuffer ) // delete if exists (just to be sure)
   499 
   403         {
   500     if(iCalSession == NULL)
   404         delete iBuffer;
   501         {
   405         iBuffer = NULL;
   502         HTI_LOG_TEXT( "CPIMHandler: Open default calendar file" );
   406         }
       
   407     if ( aData.Length() > 0 )
       
   408         {
       
   409         iBuffer = CBufFlat::NewL( aData.Length() );
       
   410         iBuffer->ExpandL( 0, aData.Length() );
       
   411         iBuffer->Ptr( 0 ).Copy( aData );
       
   412         }
       
   413 
       
   414     if ( iCalSession == NULL )
       
   415         {
       
   416         HTI_LOG_TEXT( "CPIMHandler: Creating Calendar session" );
       
   417         iCalSession = CCalSession::NewL();
   503         iCalSession = CCalSession::NewL();
   418         iCalSession->OpenL( KDefaultAgendaFile );
   504         iCalSession->OpenL( KDefaultAgendaFile );
   419         HTI_LOG_TEXT( "CPIMHandler: Calendar session open" );
   505         HTI_LOG_TEXT( "CPIMHandler: Calendar session open" );
   420         }
   506         }
   421 
   507 
   422     HTI_LOG_TEXT( "CPIMHandler: Creating entry view" );
   508     HTI_LOG_TEXT( "CPIMHandler: Creating entry view" );
   423     iEntryView = CCalEntryView::NewL( *iCalSession, *this );
   509     CCalEntryView* entryView = CCalEntryView::NewL( *iCalSession, *this );
   424     iWaiter->Start();
   510     iWaiter->Start();
       
   511     CleanupStack::PushL(entryView);
   425     if ( iEntryViewErr != KErrNone )
   512     if ( iEntryViewErr != KErrNone )
   426         {
   513         {
   427         delete iEntryView;
       
   428         iEntryView = NULL;
       
   429         delete iCalSession;
       
   430         iCalSession = NULL;
       
   431         delete iBuffer;
       
   432         iBuffer = NULL;
       
   433         User::Leave( iEntryViewErr );
   514         User::Leave( iEntryViewErr );
   434         }
   515         }
   435 
   516 
   436     // If iBuffer is NULL, no ID given, delete all calendar entries
   517     // If dataLength is 0, no ID given, delete all calendar entries
   437     if ( iBuffer == NULL )
   518     if ( dataLength == 0 )
   438         {
   519         {
   439         HTI_LOG_TEXT( "CPIMHandler: Deleting all calendar entries" );
   520         HTI_LOG_TEXT( "CPIMHandler: Deleting all calendar entries" );
   440         TCalTime minTime;
   521         TCalTime minTime;
   441         TCalTime maxTime;
   522         TCalTime maxTime;
   442         minTime.SetTimeUtcL( TCalTime::MinTime() );
   523         minTime.SetTimeUtcL( TCalTime::MinTime() );
   443         maxTime.SetTimeUtcL( TCalTime::MaxTime() );
   524         maxTime.SetTimeUtcL( TCalTime::MaxTime() );
   444         CalCommon::TCalTimeRange timeRange( minTime, maxTime );
   525         CalCommon::TCalTimeRange timeRange( minTime, maxTime );
   445         TRAPD( err, iEntryView->DeleteL( timeRange,
   526         TRAPD( err, entryView->DeleteL( timeRange,
   446                 CalCommon::EIncludeAll, *this ) );
   527                 CalCommon::EIncludeAll, *this ) );
   447         iWaiter->Start();
   528         iWaiter->Start();
   448         if ( err == KErrNone && iEntryViewErr == KErrNone )
   529         if ( err == KErrNone && iEntryViewErr == KErrNone )
   449             {
   530             {
       
   531             HTI_LOG_TEXT("All calendar entries deleted");
   450             SendOkMsgL( KNullDesC8 );
   532             SendOkMsgL( KNullDesC8 );
   451             }
   533             }
   452         else
   534         else
   453             {
   535             {
       
   536             HTI_LOG_TEXT("Failed to delete all calendar entries");
   454             SendErrorMessageL( KErrGeneral, KErrorFailedDeleteAll );
   537             SendErrorMessageL( KErrGeneral, KErrorFailedDeleteAll );
   455             }
   538             }
   456         }
   539         }
   457 
   540 
   458     // If id given, delete only calendar entry having that id
   541     // If id given, delete only calendar entry having that id
   459     else
   542     else
   460         {
   543         {
   461         TPtr8 data = iBuffer->Ptr( 0 );
   544         TCalLocalUid id = aData[0] + ( aData[1] << 8 )
   462         TCalLocalUid id = data[0] + ( data[1] << 8 )
   545                              + ( aData[2] << 16 )
   463                              + ( data[2] << 16 )
   546                              + ( aData[3] << 24 );
   464                              + ( data[3] << 24 );
       
   465         HTI_LOG_FORMAT( "CPIMHandler: Deleting one calendar entry %d", id );
   547         HTI_LOG_FORMAT( "CPIMHandler: Deleting one calendar entry %d", id );
   466         CCalEntry* entryToDelete = NULL;
   548         CCalEntry* entryToDelete = NULL;
   467         TRAPD( err, entryToDelete = iEntryView->FetchL( id ) );
   549         TRAPD( err, entryToDelete = entryView->FetchL( id ) );
   468 
   550 
   469         if ( err || entryToDelete == NULL )
   551         if ( err || entryToDelete == NULL )
   470             {
   552             {
   471             HTI_LOG_TEXT( "CPIMHandler: Calendar entry not found" );
   553             HTI_LOG_TEXT( "CPIMHandler: Calendar entry not found" );
   472             SendErrorMessageL( KErrNotFound, KErrorItemNotFound );
   554             SendErrorMessageL( KErrNotFound, KErrorItemNotFound );
   473             }
   555             }
   474         else
   556         else
   475             {
   557             {
   476             CleanupStack::PushL( entryToDelete );
   558             CleanupStack::PushL( entryToDelete );
   477             TRAP( err, iEntryView->DeleteL( *entryToDelete ) );
   559             TRAP( err, entryView->DeleteL( *entryToDelete ) );
   478             if ( err == KErrNone )
   560             if ( err == KErrNone )
   479                 {
   561                 {
       
   562                 HTI_LOG_TEXT("calendar entrie deleted");
   480                 SendOkMsgL( KNullDesC8 );
   563                 SendOkMsgL( KNullDesC8 );
   481                 }
   564                 }
   482             else
   565             else
   483                 {
   566                 {
   484                 HTI_LOG_TEXT( "CPIMHandler: Error deleting calendar entry" )
   567                 HTI_LOG_TEXT( "Failed to delete calendar entry" )
   485                 SendErrorMessageL( KErrGeneral, KErrorFailedDelete );
   568                 SendErrorMessageL( KErrGeneral, KErrorFailedDelete );
   486                 }
   569                 }
   487             CleanupStack::PopAndDestroy( entryToDelete );
   570             CleanupStack::PopAndDestroy( entryToDelete );
   488             }
   571             }
   489         }
   572         }
   490     delete iEntryView;
   573     CleanupStack::PopAndDestroy(); //entryView;
   491     iEntryView = NULL;
       
   492     delete iCalSession;
       
   493     iCalSession = NULL;
       
   494     delete iBuffer;
       
   495     iBuffer = NULL;
       
   496     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCalendarDeleteFuncL" );
   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" );
   497     }
   718     }
   498 
   719 
   499 // ----------------------------------------------------------------------------
   720 // ----------------------------------------------------------------------------
   500 void CPIMHandler::SendOkMsgL( const TDesC8& aData )
   721 void CPIMHandler::SendOkMsgL( const TDesC8& aData )
   501     {
   722     {
   523     iIsBusy = EFalse;
   744     iIsBusy = EFalse;
   524     HTI_LOG_FUNC_OUT( "CPIMHandler::SendErrorMessageL: Done" );
   745     HTI_LOG_FUNC_OUT( "CPIMHandler::SendErrorMessageL: Done" );
   525     }
   746     }
   526 
   747 
   527 // ----------------------------------------------------------------------------
   748 // ----------------------------------------------------------------------------
   528 TInt CallNpdHlp( const TDesC& aCmd )
       
   529     {
       
   530     HTI_LOG_FUNC_IN( "CallNpdHlp" );
       
   531 
       
   532     RProcess HtiNpdHlp;
       
   533     TInt err = HtiNpdHlp.Create( KHtiNpdHlpExe, aCmd );
       
   534     if ( err )
       
   535         {
       
   536         HTI_LOG_FORMAT( "Could not create HtiNpdHlp.Exe process %d", err );
       
   537         return err;
       
   538         }
       
   539 
       
   540     TRequestStatus status;
       
   541     HtiNpdHlp.Logon( status );
       
   542     HtiNpdHlp.Resume();
       
   543     User::WaitForRequest( status );
       
   544     if ( status.Int() != KErrNone )
       
   545         {
       
   546         HTI_LOG_FORMAT( "status     %d", status.Int() );
       
   547         HTI_LOG_FORMAT( "ExitReason %d", HtiNpdHlp.ExitReason() );
       
   548         HTI_LOG_FORMAT( "ExitType   %d", HtiNpdHlp.ExitType() );
       
   549         HtiNpdHlp.Close();
       
   550         return status.Int();
       
   551         }
       
   552 
       
   553     HtiNpdHlp.Close();
       
   554 
       
   555     HTI_LOG_FUNC_OUT( "CallNpdHlp" );
       
   556     return KErrNone;
       
   557     }
       
   558 
       
   559 // ----------------------------------------------------------------------------
       
   560 void CPIMHandler::SendNotepadOkMsgL( CHtiPIMServicePlugin::TCommand aCommand )
       
   561     {
       
   562     HTI_LOG_FUNC_IN( "CPIMHandler::SendNotepadOkMsgL" );
       
   563     TBuf8<1> msg;
       
   564     msg.Append( aCommand );
       
   565     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
   566                         msg.AllocL(), KPIMServiceUid ) );
       
   567     iIsBusy = EFalse;
       
   568     HTI_LOG_FUNC_OUT( "CPIMHandler::SendNotepadOkMsgL" );
       
   569     }
       
   570 
       
   571 // ----------------------------------------------------------------------------
       
   572 void CPIMHandler::HandleNotepadAddMemoFuncL( const TDesC8& aData )
       
   573     {
       
   574     HTI_LOG_FUNC_IN( "CPIMHandler::HandleNotepadAddMemoFuncL" );
       
   575 
       
   576     if ( aData.Length() < 1 )
       
   577         {
       
   578         SendErrorMessageL( KErrArgument, KErrorMissingText );
       
   579         return;
       
   580         }
       
   581 
       
   582     // convert text from TDesC8 -> TDesC
       
   583     // expecting the input TDesC8 contains UTF-8 data
       
   584     HBufC* text = CnvUtfConverter::ConvertToUnicodeFromUtf8L( aData );
       
   585     HTI_LOG_TEXT( "CPIMHandler: Conversion to Unicode done" );
       
   586     CleanupStack::PushL( text );
       
   587 
       
   588     HBufC* cmd = HBufC::NewLC( KCmdAddMemo().Length() + 1 + ( *text ).Length() );
       
   589     cmd->Des().Copy( KCmdAddMemo );
       
   590     cmd->Des().Append( KCmdDelim );
       
   591     cmd->Des().Append( *text );
       
   592 
       
   593     TInt err = CallNpdHlp( *cmd );
       
   594     if ( err )
       
   595         {
       
   596         SendErrorMessageL( err, KErrorNotepadAddMemoFailed );
       
   597         }
       
   598     else
       
   599         {
       
   600         SendNotepadOkMsgL( CHtiPIMServicePlugin::ENotepadAddMemo );
       
   601         }
       
   602 
       
   603     CleanupStack::PopAndDestroy( 2 ); // text, cmd
       
   604 
       
   605     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleNotepadAddMemoFuncL" );
       
   606     }
       
   607 
       
   608 // ----------------------------------------------------------------------------
       
   609 void CPIMHandler::HandleNotepadAddMemoFromFileFuncL( const TDesC8& aData )
       
   610     {
       
   611     HTI_LOG_FUNC_IN( "CPIMHandler::HandleNotepadAddMemoFromFileFuncL" );
       
   612 
       
   613     if ( aData.Length() < 1 )
       
   614         {
       
   615         SendErrorMessageL( KErrArgument, KErrorMissingFilepath );
       
   616         return;
       
   617         }
       
   618 
       
   619     // convert filename from TDesC8 -> TDesC
       
   620     // expecting the input TDesC8 contains UTF-8 data
       
   621     HBufC* filename = CnvUtfConverter::ConvertToUnicodeFromUtf8L( aData );
       
   622     HTI_LOG_TEXT( "CPIMHandler: Conversion to Unicode done" );
       
   623     CleanupStack::PushL( filename );
       
   624 
       
   625     HBufC* cmd = HBufC::NewLC( KCmdAddMemoFromFile().Length() + 1 + ( *filename ).Length() );
       
   626     cmd->Des().Copy( KCmdAddMemoFromFile );
       
   627     cmd->Des().Append( KCmdDelim );
       
   628     cmd->Des().Append( *filename );
       
   629 
       
   630     TInt err = CallNpdHlp( *cmd );
       
   631     if ( err )
       
   632         {
       
   633         SendErrorMessageL( err, KErrorNotepadAddMemoFromFileFailed );
       
   634         }
       
   635     else
       
   636         {
       
   637         SendNotepadOkMsgL( CHtiPIMServicePlugin::ENotepadAddMemoFromFile );
       
   638         }
       
   639 
       
   640     CleanupStack::PopAndDestroy( 2 ); // filename, cmd
       
   641 
       
   642     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleNotepadAddMemoFromFileFuncL" );
       
   643     }
       
   644 
       
   645 // ----------------------------------------------------------------------------
       
   646 void CPIMHandler::HandleNotepadDeleteAllFuncL()
       
   647     {
       
   648     HTI_LOG_FUNC_IN( "CPIMHandler::HandleNotepadDeleteAllFuncL" );
       
   649 
       
   650     TInt err = CallNpdHlp( KCmdDeleteAll() );
       
   651     if ( err )
       
   652         {
       
   653         SendErrorMessageL( err, KErrorNotepadDeleteAllFailed );
       
   654         }
       
   655     else
       
   656         {
       
   657         SendNotepadOkMsgL( CHtiPIMServicePlugin::ENotepadDeleteAll );
       
   658         }
       
   659 
       
   660     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleNotepadDeleteAllFuncL" );
       
   661     }
       
   662 
       
   663 // ----------------------------------------------------------------------------
       
   664 // CPIMHandler::CreateContactDeleteViewL
       
   665 // Creates a contact view containing the contacts to be deleted.
       
   666 // ----------------------------------------------------------------------------
       
   667 void CPIMHandler::CreateContactDeleteViewL()
       
   668     {
       
   669     HTI_LOG_FUNC_IN( "CPIMHandler::CreateContactDeleteViewL" );
       
   670 
       
   671     if ( iContactView )
       
   672         {
       
   673         iContactView->RemoveObserver( *this );
       
   674         delete iContactView;
       
   675         iContactView = NULL;
       
   676         }
       
   677 
       
   678     CVPbkContactViewDefinition* viewDef = CVPbkContactViewDefinition::NewL();
       
   679     CleanupStack::PushL( viewDef );
       
   680     viewDef->SetType( EVPbkContactsView );
       
   681     viewDef->SetSharing( EVPbkLocalView );
       
   682     viewDef->SetSortPolicy( EVPbkSortedContactView );
       
   683     CVPbkSortOrder* sortOrder = CVPbkSortOrder::NewL(
       
   684             iContactStore->StoreProperties().SupportedFields() );
       
   685     CleanupStack::PushL( sortOrder );
       
   686     iContactView = iContactStore->CreateViewLC( *viewDef, *this, *sortOrder );
       
   687     CleanupStack::Pop(); // view;
       
   688     CleanupStack::PopAndDestroy( 2 ); // sortOrder, viewDef
       
   689 
       
   690     HTI_LOG_FUNC_OUT( "CPIMHandler::CreateContactDeleteViewL" );
       
   691     }
       
   692 
       
   693 // ----------------------------------------------------------------------------
       
   694 // CPIMHandler::DeleteContactsInViewL
       
   695 // Deletes the contacts that are currently in iContactView.
       
   696 // ----------------------------------------------------------------------------
       
   697 void CPIMHandler::DeleteContactsInViewL()
       
   698     {
       
   699     HTI_LOG_FUNC_IN( "CPIMHandler::DeleteContactsInViewL" );
       
   700 
       
   701     TInt cntCount( iContactView->ContactCountL() );
       
   702     HTI_LOG_FORMAT( "Contact count in view = %d", cntCount );
       
   703     if ( cntCount > 0 )
       
   704         {
       
   705         CVPbkContactLinkArray* contactLinks = CVPbkContactLinkArray::NewLC();
       
   706         for ( TInt i = 0; i < cntCount; ++i )
       
   707             {
       
   708             MVPbkContactLink* link =
       
   709                 iContactView->ContactAtL( i ).CreateLinkLC();
       
   710             contactLinks->AppendL( link );
       
   711             CleanupStack::Pop(); // link
       
   712             }
       
   713         // Following DeleteContactsL will result in calls to StepComplete
       
   714         // and finally OperationComplete (StepFailed if error occurs)
       
   715         iOp = iContactManager->DeleteContactsL( *contactLinks, *this );
       
   716         CleanupStack::PopAndDestroy(); // contactLinks
       
   717         }
       
   718     else
       
   719         {
       
   720         // Nothing to delete
       
   721         iContactStore->Close( *this );
       
   722         SendOkMsgL( KNullDesC8 );
       
   723         }
       
   724 
       
   725     // We don't need the view anymore
       
   726     HTI_LOG_TEXT( "Deleting the contact view" );
       
   727     iContactView->RemoveObserver( *this );
       
   728     delete iContactView;
       
   729     iContactView = NULL;
       
   730 
       
   731     HTI_LOG_FUNC_OUT( "CPIMHandler::DeleteContactsInViewL" );
       
   732     }
       
   733 
       
   734 // ----------------------------------------------------------------------------
       
   735 // CPIMHandler::StoreReady
       
   736 // Called when a contact store is ready to use.
       
   737 // From MVPbkContactStoreObserver
       
   738 // ----------------------------------------------------------------------------
       
   739 void CPIMHandler::StoreReady( MVPbkContactStore& aContactStore )
       
   740     {
       
   741     HTI_LOG_FUNC_IN( "CPIMHandler::StoreReady" );
       
   742     if ( iIsBusy && iContactStore == &aContactStore )
       
   743         {
       
   744         if ( iCommand == CHtiPIMServicePlugin::EImportVCard )
       
   745             {
       
   746             iReadStream.Open( *iBuffer, 0 );
       
   747             HTI_LOG_TEXT( "Starting vCard import" );
       
   748             TRAPD( err, iOp = iVCardEngine->ImportVCardL(
       
   749                     *iContactStore, iReadStream, *this ) );
       
   750             HTI_LOG_FORMAT( "ImpoortVCardL returned %d", err );
       
   751             if ( err != KErrNone )
       
   752                 {
       
   753                 delete iOp;
       
   754                 iOp = NULL;
       
   755                 iReadStream.Close();
       
   756                 delete iBuffer;
       
   757                 iBuffer = NULL;
       
   758                 iContactStore->Close( *this );
       
   759                 TRAP_IGNORE( SendErrorMessageL( err, KErrorVCardImportFailed ) );
       
   760                 }
       
   761             }
       
   762         else if ( iCommand == CHtiPIMServicePlugin::EDeleteContact )
       
   763             {
       
   764             TRAPD( err, CreateContactDeleteViewL() );
       
   765             if ( err != KErrNone )
       
   766                 {
       
   767                 iContactStore->Close( *this );
       
   768                 TRAP_IGNORE( SendErrorMessageL( err, KErrorFailedDelete ) );
       
   769                 }
       
   770             }
       
   771         }
       
   772     HTI_LOG_FUNC_OUT( "CPIMHandler::StoreReady" );
       
   773     }
       
   774 
       
   775 // ----------------------------------------------------------------------------
       
   776 // CPIMHandler::StoreUnavailable
       
   777 // Called when a contact store becomes unavailable.
       
   778 // From MVPbkContactStoreObserver
       
   779 // ----------------------------------------------------------------------------
       
   780 void CPIMHandler::StoreUnavailable( MVPbkContactStore& aContactStore,
       
   781         TInt aReason )
       
   782     {
       
   783     HTI_LOG_FUNC_IN( "CPIMHandler::StoreUnavailable" );
       
   784 
       
   785     if ( iIsBusy && iContactStore == &aContactStore )
       
   786         {
       
   787         delete iBuffer;
       
   788         iBuffer = NULL;
       
   789         TRAP_IGNORE( SendErrorMessageL( aReason, KErrorVCardImportFailed ) );
       
   790         }
       
   791 
       
   792     HTI_LOG_FUNC_OUT( "CPIMHandler::StoreUnavailable" );
       
   793     }
       
   794 
       
   795 // ----------------------------------------------------------------------------
       
   796 // CPIMHandler::HandleStoreEventL
       
   797 // Called when changes occur in the contact store.
       
   798 // From MVPbkContactStoreObserver
       
   799 // ----------------------------------------------------------------------------
       
   800 void CPIMHandler::HandleStoreEventL( MVPbkContactStore& /*aContactStore*/,
       
   801                         TVPbkContactStoreEvent /*aStoreEvent*/ )
       
   802     {
       
   803     HTI_LOG_FUNC_IN( "CPIMHandler::HandleStoreEventL" );
       
   804     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleStoreEventL" );
       
   805     }
       
   806 
       
   807 // ----------------------------------------------------------------------------
       
   808 // CPIMHandler::ContactsSaved
       
   809 // Called when the contact has been successfully commited or copied.
       
   810 // From MVPbkContactCopyObserver
       
   811 // ----------------------------------------------------------------------------
       
   812 void CPIMHandler::ContactsSaved( MVPbkContactOperationBase& aOperation,
       
   813         MVPbkContactLinkArray* aResults )
       
   814 {
       
   815     HTI_LOG_FUNC_IN( "CPIMHandler::ContactsSaved" );
       
   816 
       
   817     if ( iIsBusy && iOp == &aOperation )
       
   818         {
       
   819         TInt count = aResults->Count();
       
   820         HTI_LOG_FORMAT( "%d contact(s) added", count );
       
   821         delete aResults;
       
   822         delete iOp;
       
   823         iOp = NULL;
       
   824         iReadStream.Close();
       
   825         delete iBuffer;
       
   826         iBuffer = NULL;
       
   827         iContactStore->Close( *this );
       
   828         TInt entryId = 0; // We can't get the ID, just send zero
       
   829         TBuf8<4> idBuf;
       
   830         idBuf.Append( ( TUint8* ) &entryId, 4 );
       
   831         TRAP_IGNORE( SendOkMsgL( idBuf ) );
       
   832         }
       
   833 
       
   834     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactsSaved" );
       
   835 }
       
   836 
       
   837 // ----------------------------------------------------------------------------
       
   838 // CPIMHandler::ContactsSavingFailed
       
   839 // Called when there was en error while saving contact(s).
       
   840 // From MVPbkContactCopyObserver
       
   841 // ----------------------------------------------------------------------------
       
   842 void CPIMHandler::ContactsSavingFailed(
       
   843         MVPbkContactOperationBase& aOperation, TInt aError )
       
   844 {
       
   845     HTI_LOG_FUNC_IN( "CPIMHandler::ContactsSavingFailed" );
       
   846 
       
   847     if ( iIsBusy && iOp == &aOperation )
       
   848         {
       
   849         delete iOp;
       
   850         iOp = NULL;
       
   851         iReadStream.Close();
       
   852         delete iBuffer;
       
   853         iBuffer = NULL;
       
   854         iContactStore->Close( *this );
       
   855         TRAP_IGNORE( SendErrorMessageL( aError, KErrorVCardImportFailed ) );
       
   856         }
       
   857 
       
   858     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactsSavingFailed" );
       
   859 }
       
   860 
       
   861 // ----------------------------------------------------------------------------
       
   862 // CPIMHandler::ContactViewReady
       
   863 // Called when a view is ready for use.
       
   864 // From MVPbkContactViewObserver
       
   865 // ----------------------------------------------------------------------------
       
   866 void CPIMHandler::ContactViewReady( MVPbkContactViewBase& aView )
       
   867     {
       
   868     HTI_LOG_FUNC_IN( "CPIMHandler::ContactViewReady" );
       
   869 
       
   870     if ( iContactView == &aView && iIsBusy &&
       
   871             iCommand == CHtiPIMServicePlugin::EDeleteContact )
       
   872         {
       
   873         TRAPD( err, DeleteContactsInViewL() );
       
   874         if ( err != KErrNone )
       
   875             {
       
   876             TRAP_IGNORE( SendErrorMessageL( err, KErrorFailedDelete ) );
       
   877             }
       
   878         }
       
   879 
       
   880     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactViewReady" );
       
   881     }
       
   882 
       
   883 // ----------------------------------------------------------------------------
       
   884 // CHtiSimDirHandlerVPbk::ContactViewUnavailable
       
   885 // Called when a view is unavailable for a while.
       
   886 // From MVPbkContactViewObserver
       
   887 // ----------------------------------------------------------------------------
       
   888 void CPIMHandler::ContactViewUnavailable( MVPbkContactViewBase& /*aView*/ )
       
   889     {
       
   890     HTI_LOG_FUNC_IN( "CPIMHandler::ContactViewUnavailable" );
       
   891     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactViewUnavailable" );
       
   892     }
       
   893 
       
   894 // ----------------------------------------------------------------------------
       
   895 // CPIMHandler::ContactAddedToView
       
   896 // Called when a contact has been added to the view.
       
   897 // From MVPbkContactViewObserver
       
   898 // ----------------------------------------------------------------------------
       
   899 void CPIMHandler::ContactAddedToView( MVPbkContactViewBase& /*aView*/,
       
   900         TInt /*aIndex*/, const MVPbkContactLink& /*aContactLink*/ )
       
   901     {
       
   902     HTI_LOG_FUNC_IN( "CPIMHandler::ContactAddedToView" );
       
   903     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactAddedToView" );
       
   904     }
       
   905 
       
   906 // ----------------------------------------------------------------------------
       
   907 // CPIMHandler::ContactRemovedFromView
       
   908 // Called when a contact has been removed from a view.
       
   909 // From MVPbkContactViewObserver
       
   910 // ----------------------------------------------------------------------------
       
   911 void CPIMHandler::ContactRemovedFromView( MVPbkContactViewBase& /*aView*/,
       
   912         TInt /*aIndex*/, const MVPbkContactLink& /*aContactLink*/ )
       
   913     {
       
   914     HTI_LOG_FUNC_IN( "CPIMHandler::ContactRemovedFromView" );
       
   915     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactRemovedFromView" );
       
   916     }
       
   917 
       
   918 // ----------------------------------------------------------------------------
       
   919 // CPIMHandler::ContactViewError
       
   920 // Called when an error occurs in the view.
       
   921 // From MVPbkContactViewObserver
       
   922 // ----------------------------------------------------------------------------
       
   923 void CPIMHandler::ContactViewError( MVPbkContactViewBase& aView,
       
   924         TInt aError, TBool aErrorNotified )
       
   925     {
       
   926     HTI_LOG_FUNC_IN( "CPIMHandler::ContactViewError" );
       
   927     HTI_LOG_FORMAT( "CPIMHandler::ContactViewError: %d", aError );
       
   928     HTI_LOG_FORMAT( "ErrorNotified = %d", aErrorNotified );
       
   929     if ( iContactView == &aView )
       
   930         {
       
   931         iContactView->RemoveObserver( *this );
       
   932         delete iContactView;
       
   933         iContactView = NULL;
       
   934         if ( iIsBusy && iCommand == CHtiPIMServicePlugin::EDeleteContact )
       
   935             {
       
   936             SendErrorMessageL( aError, KErrorFailedDelete );
       
   937             }
       
   938         iContactStore->Close( *this );
       
   939         }
       
   940     aErrorNotified = aErrorNotified;  // avoid compiler warning
       
   941     HTI_LOG_FUNC_OUT( "CPIMHandler::ContactViewError" );
       
   942     }
       
   943 
       
   944 // ----------------------------------------------------------------------------
       
   945 // CPIMHandler::StepComplete
       
   946 // Called when one step of the batch operation is complete.
       
   947 // From MVPbkBatchOperationObserver
       
   948 // ----------------------------------------------------------------------------
       
   949 void CPIMHandler::StepComplete( MVPbkContactOperationBase& /*aOperation*/,
       
   950                            TInt /*aStepSize*/ )
       
   951     {
       
   952     HTI_LOG_FUNC_IN( "CPIMHandler::StepComplete" );
       
   953     HTI_LOG_FUNC_OUT( "CPIMHandler::StepComplete" );
       
   954     }
       
   955 
       
   956 // ----------------------------------------------------------------------------
       
   957 // CPIMHandler::StepFailed
       
   958 // Called when one step of the batch operation fails.
       
   959 // From MVPbkBatchOperationObserver
       
   960 // ----------------------------------------------------------------------------
       
   961 TBool CPIMHandler::StepFailed( MVPbkContactOperationBase& aOperation,
       
   962                                          TInt /*aStepSize*/, TInt aError )
       
   963     {
       
   964     HTI_LOG_FUNC_IN( "CPIMHandler::StepFailed" );
       
   965     HTI_LOG_FORMAT( "Error %d", aError );
       
   966     if ( iOp == &aOperation )
       
   967         {
       
   968         // We are returning EFalse (= do not continue) we can delete
       
   969         // the operation. OperationComplete() won't be called.
       
   970         delete iOp;
       
   971         iOp = NULL;
       
   972         iContactStore->Close( *this );
       
   973         TRAP_IGNORE( SendErrorMessageL( aError, KErrorFailedDelete ) );
       
   974         }
       
   975     HTI_LOG_FUNC_OUT( "CPIMHandler::StepFailed" );
       
   976     return EFalse; // do not continue
       
   977     }
       
   978 
       
   979 // ----------------------------------------------------------------------------
       
   980 // CPIMHandler::OperationComplete
       
   981 // Called when operation is completed.
       
   982 // From MVPbkBatchOperationObserver
       
   983 // ----------------------------------------------------------------------------
       
   984 void CPIMHandler::OperationComplete(
       
   985             MVPbkContactOperationBase& aOperation )
       
   986     {
       
   987     HTI_LOG_FUNC_IN( "CPIMHandler::OperationComplete" );
       
   988     // Operation is complete -> delete it
       
   989     if ( iOp == &aOperation )
       
   990         {
       
   991         delete iOp;
       
   992         iOp = NULL;
       
   993         iContactStore->Close( *this );
       
   994         if ( iIsBusy && iCommand == CHtiPIMServicePlugin::EDeleteContact )
       
   995             {
       
   996             // Delete operation has completed
       
   997             TRAP_IGNORE( SendOkMsgL( KNullDesC8 ) );
       
   998             }
       
   999         }
       
  1000     HTI_LOG_FUNC_OUT( "CPIMHandler::OperationComplete" );
       
  1001     }
       
  1002 
       
  1003 
       
  1004 // ----------------------------------------------------------------------------
       
  1005 // CPIMHandler::Progress
   749 // CPIMHandler::Progress
  1006 // Called during calendar entry view creation and operations.
   750 // Called during calendar entry view creation and operations.
  1007 // Called only if NotifyProgress returns ETrue.
   751 // Called only if NotifyProgress returns ETrue.
  1008 // From MCalProgressCallBack
   752 // From MCalProgressCallBack
  1009 // ----------------------------------------------------------------------------
   753 // ----------------------------------------------------------------------------