omadm/lawmo/lawmoplugins/contactsplugin/src/lawmocontactsasyncwipe.cpp
branchRCL_3
changeset 23 c4687ff85147
child 26 ae4a65edc4fe
equal deleted inserted replaced
22:9360ca28b668 23:c4687ff85147
       
     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:  Active Object Source for wiping contacts, used by lawmocontactsplugin.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <CPbkContactEngine.h>
       
    20 #include <CPbkSingleEntryFetchDlg.h>
       
    21 #include <rpbkviewresourcefile.h>
       
    22 #include <cntdb.h>
       
    23 #include <etelmm.h>
       
    24 
       
    25 // PhoneBook2 uid 
       
    26  #include <Pbk2UID.h>
       
    27 
       
    28 // Application Closing
       
    29 #include <apgtask.h>
       
    30 #include <w32std.h>
       
    31 #include <APGTASK.H>
       
    32 
       
    33 #include "fdebug.h"
       
    34 #include "lawmocontactsasyncwipe.h"
       
    35 
       
    36 // ----------------------------------------------------------------------------- 
       
    37 // CActiveWipeObject::CActiveWipeObject
       
    38 // Description: CActiveWipeObject constructer . Initializes datamember of MLawmoPluginWipeObserver 
       
    39 // ----------------------------------------------------------------------------- 
       
    40 //
       
    41 CActiveWipeObject::CActiveWipeObject(MLawmoPluginWipeObserver* aObserver) : CActive( EPriorityStandard ) , 
       
    42         iObserver(aObserver), iContactsDb (NULL), iContacts (NULL)
       
    43 {
       
    44     FLOG(_L( " Contacts: CActiveWipeObject::CActiveWipeObject(MLawmoPluginWipeObserver* aObserver) >> "));
       
    45     // See ConstructL() for initialisation completion.
       
    46     FLOG(_L( " Contacts: CActiveWipeObject::CActiveWipeObject(MLawmoPluginWipeObserver* aObserver) << "));
       
    47 }
       
    48 
       
    49 // ----------------------------------------------------------------------------- 
       
    50 // CActiveWipeObject::NewL
       
    51 // Description: CActiveWipeObject constructer 
       
    52 // ----------------------------------------------------------------------------- 
       
    53 //
       
    54 CActiveWipeObject* CActiveWipeObject::NewL(MLawmoPluginWipeObserver* aObserver)
       
    55 {
       
    56     FLOG(_L( " Contacts: CActiveWipeObject::NewL(MLawmoPluginWipeObserver* aObserver) >> "));
       
    57     CActiveWipeObject* self=new(ELeave) CActiveWipeObject(aObserver);  
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL(); 
       
    60     CleanupStack::Pop();
       
    61     FLOG(_L( " Contacts: CActiveWipeObject::NewL(MLawmoPluginWipeObserver* aObserver) << "));
       
    62     return self;
       
    63 }
       
    64 
       
    65 // ----------------------------------------------------------------------------------------
       
    66 // CActiveWipeObject::ConstructL
       
    67 // ----------------------------------------------------------------------------------------
       
    68 void CActiveWipeObject::ConstructL()
       
    69 {   
       
    70     FLOG(_L( " Contacts: CActiveWipeObject::ConstructL >> "));
       
    71     iTimer.CreateLocal();
       
    72     User::LeaveIfError( iWsSession.Connect() );
       
    73     CActiveScheduler::Add(this); // Add to scheduler
       
    74     FLOG(_L( " Contacts: CActiveWipeObject::ConstructL << "));
       
    75 }
       
    76 
       
    77 //
       
    78 //----------------------------------------------------------------------------- 
       
    79 // CActiveWipeObject:: CActiveWipeObject
       
    80 // Description: CActiveWipeObject Destructer
       
    81 // ----------------------------------------------------------------------------- 
       
    82 // 
       
    83 CActiveWipeObject::~CActiveWipeObject()
       
    84 {
       
    85     FLOG(_L( " Contacts: CActiveWipeObject::~CActiveWipeObject() >> "));
       
    86     Cancel();
       
    87 
       
    88     if(iContactsDb)
       
    89     {
       
    90         FLOG(_L("  delete iContactsDb;"));
       
    91         delete iContactsDb;
       
    92     }
       
    93         
       
    94     iTimer.Close();
       
    95     
       
    96     iWsSession.Close();
       
    97 
       
    98     FLOG(_L( " Contacts: CActiveWipeObject::~CActiveWipeObject() << "));
       
    99 }
       
   100 
       
   101 //
       
   102 //----------------------------------------------------------------------------- 
       
   103 // CActiveWipeObject::WipeAsyncL
       
   104 // Description: Wipes the Contacts 
       
   105 // ---------------------------------------------------------------------------- 
       
   106 //
       
   107 
       
   108 void CActiveWipeObject::WipeAsyncL ()
       
   109 {    
       
   110     FLOG(_L(" Contacts: void CActiveWipeObject::WipeAsyncL () >>"));
       
   111 
       
   112     if ( !WaitTillPhoneBookKilled()) //Application is closed already
       
   113         {
       
   114         TRequestStatus* status = &iStatus;
       
   115         FLOG(_L("  SetActive()"));
       
   116         SetActive();
       
   117 		User::RequestComplete(status,KErrNone);
       
   118         }
       
   119       
       
   120     FLOG(_L(" Contacts: void CActiveWipeObject::WipeAsyncL () <<"));
       
   121 }
       
   122 
       
   123 //
       
   124 //----------------------------------------------------------------------------- 
       
   125 // CActiveWipeObject::RunError
       
   126 // Description: This returns error 
       
   127 //----------------------------------------------------------------------------- 
       
   128 //
       
   129 
       
   130 TInt CActiveWipeObject::RunError(TInt aError)
       
   131 {
       
   132     FLOG(_L(" Contacts: TInt CActiveWipeObject::RunError(TInt aError) >>"));
       
   133     FLOG(_L("  iObserver->HandleWipeCompleted(aError);"));
       
   134     iObserver->HandleWipeCompleted(aError);
       
   135     FLOG(_L(" Contacts: TInt CActiveWipeObject::RunError(TInt aError) <<"));
       
   136     return KErrNone;
       
   137 }
       
   138 
       
   139 //
       
   140 //----------------------------------------------------------------------------- 
       
   141 // CActiveWipeObject::DoCancel
       
   142 // Description: This calls CActive's Cancel 
       
   143 //----------------------------------------------------------------------------- 
       
   144 //
       
   145 void CActiveWipeObject::DoCancel()
       
   146 {
       
   147     FLOG(_L(" Contacts: void CActiveWipeObject::DoCancel() >>"));
       
   148     iTimer.Cancel();
       
   149     FLOG(_L(" Contacts: void CActiveWipeObject::DoCancel() <<"));
       
   150 }
       
   151 
       
   152 void CActiveWipeObject::DeletePhoneContactsL()
       
   153 {
       
   154     FLOG(_L(" Contacts: void CActiveWipeObject::DeletePhoneContactsL() >>"));
       
   155 
       
   156     iContactsDb = CContactDatabase::OpenL();
       
   157     iContactsDb->SetDbViewContactType( KUidContactCard );
       
   158     iContacts= iContactsDb->SortedItemsL();
       
   159     FLOG(_L("Count = %d "), iContacts->Count());
       
   160     iContactsDb->DeleteContactsL(*iContacts);
       
   161     FLOG(_L(" Contacts: void CActiveWipeObject::DeletePhoneContactsL() <<"));
       
   162 }
       
   163 
       
   164 void CActiveWipeObject::DeleteSIMContactsL()
       
   165 {
       
   166     FLOG(_L("void CActiveWipeObject::DeleteSIMContactsL() >> "));
       
   167     RTelServer telServer;
       
   168     RMobilePhone phone;
       
   169     RTelServer::TPhoneInfo info;
       
   170     User::LeaveIfError(telServer.Connect());
       
   171     CleanupClosePushL(telServer);
       
   172     
       
   173     User::LeaveIfError(telServer.GetPhoneInfo(0,info));
       
   174     User::LeaveIfError(phone.Open(telServer,info.iName) );
       
   175     CleanupClosePushL(phone);
       
   176     
       
   177     RMobilePhoneBookStore simPhBk;
       
   178     User::LeaveIfError(simPhBk.Open(phone, KETelIccAdnPhoneBook)) ;
       
   179     CleanupClosePushL(simPhBk); 
       
   180 
       
   181     TRequestStatus aStatus;
       
   182     FLOG(_L("  Contacts: phonebookstore open success"));
       
   183     simPhBk.DeleteAll(aStatus);
       
   184     FLOG(_L("  Contacts: issued delete"));
       
   185     User::WaitForRequest(aStatus);
       
   186     FLOG(_L("  Contacts: waiting done"));
       
   187 
       
   188  		User::LeaveIfError(aStatus.Int());
       
   189     CleanupStack::PopAndDestroy(&simPhBk);
       
   190     CleanupStack::PopAndDestroy(&phone);
       
   191     CleanupStack::PopAndDestroy(&telServer);
       
   192       
       
   193     FLOG(_L("void CActiveWipeObject::DeleteSIMContactsL() << "));
       
   194 }
       
   195 
       
   196 TBool CActiveWipeObject::WaitTillPhoneBookKilled()
       
   197     {
       
   198     FLOG(_L(" void CTTPAPITestAppUi::WaitTillPhonebookKilled() >>"));
       
   199     TBool ret(EFalse);
       
   200     TBool flag(ETrue);
       
   201 
       
   202     TApaTaskList list(iWsSession);
       
   203     TUid uid =
       
   204         {
       
   205         KPbk2UID3
       
   206         };
       
   207 
       
   208     while (flag)
       
   209         {
       
   210         TApaTask task(list.FindApp(uid));
       
   211         if (task.Exists())
       
   212             {
       
   213             FLOG(_L("Killing one instance..."));
       
   214             task.EndTask();
       
   215             User::After(500000);
       
   216             ret = ETrue;
       
   217             }
       
   218         else
       
   219             {
       
   220             flag = EFalse;
       
   221             }
       
   222         }
       
   223     if (ret)
       
   224         {
       
   225         FLOG(_L(" WaitTillPhonebookKilled: iTimer.After(iStatus,1000); "));
       
   226         iTimer.After(iStatus, 2000); // 0.002 sec
       
   227         SetActive();
       
   228         }
       
   229     
       
   230      FLOG(_L(" void CTTPAPITestAppUi::WaitTillPhonebookKilled(), ret = %d <<"), ret);
       
   231      return ret;
       
   232     }
       
   233 
       
   234 //
       
   235 //----------------------------------------------------------------------------- 
       
   236 // CActiveWipeObject::RunL 
       
   237 // Description: CActive::RunL implementation which will either stop the
       
   238 // Scheduler or increment the count
       
   239 //----------------------------------------------------------------------------- 
       
   240 //
       
   241 void CActiveWipeObject::RunL()
       
   242     {
       
   243     FLOG(_L(" Contacts: void CActiveWipeObject::RunL() >>"));
       
   244 
       
   245     DeletePhoneContactsL();
       
   246     DeleteSIMContactsL();
       
   247     iObserver->HandleWipeCompleted(KErrNone);
       
   248     FLOG(_L(" Contacts: void CActiveWipeObject::RunL() <<"));
       
   249     }