phonebookui/Phonebook/App/src/CPbkDeleteContactsCmd.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *           Provides phonebook delete contacts command class methods.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbkDeleteContactsCmd.h"
       
    22 #include <Phonebook.rsg>
       
    23 #include <eikprogi.h>
       
    24 #include <CPbkProgressNoteWrapper.h>
       
    25 #include <CPbkContactViewListControl.h>
       
    26 #include <CPbkContactEngine.h>
       
    27 #include <cntdb.h>
       
    28 
       
    29 #include "CPbkAppUi.h"
       
    30 #include <MPbkCommandObserver.h>
       
    31 
       
    32 #include <pbkdebug.h>
       
    33 
       
    34 /// Unnamed namespace for local definitions
       
    35 namespace {
       
    36 
       
    37 // LOCAL DEBUG CODE
       
    38 #ifdef _DEBUG
       
    39 enum TPanicCode
       
    40     {
       
    41     EPanicPreCond_ResetWhenDestroyed = 1
       
    42     };
       
    43 
       
    44 void Panic(TPanicCode aReason)
       
    45     {
       
    46     _LIT(KPanicText, "CPbkDeleteContactsCmd");
       
    47     User::Panic(KPanicText,aReason);
       
    48     }
       
    49 #endif
       
    50 
       
    51 // LOCAL CONSTANTS
       
    52 const TInt KB = 1024; // kB
       
    53 const TInt PbkFreeSpaceForDelete = 8192; // 8kB
       
    54 
       
    55 }
       
    56 
       
    57 
       
    58 // ================= MEMBER FUNCTIONS =======================
       
    59 
       
    60 inline CPbkDeleteContactsCmd::CPbkDeleteContactsCmd
       
    61         (CPbkContactEngine& aEngine,
       
    62         CPbkContactViewListControl& aUiControl) :
       
    63     CActive(EPriorityIdle),
       
    64     iEngine(aEngine),
       
    65     iUiControl(aUiControl),
       
    66     iFocusId(KNullContactId),
       
    67     iFreeSpaceRequiredToDelete(PbkFreeSpaceForDelete),  // 8 kB
       
    68     iDialogDismissed(ETrue) // there is no dialog at this point
       
    69     {
       
    70     PBK_DEBUG_PRINT
       
    71         (PBK_DEBUG_STRING("CPbkDeleteContactsCmd::CPbkDeleteContactsCmd(0x%x)"),
       
    72         this);
       
    73 
       
    74     CActiveScheduler::Add(this);
       
    75     }
       
    76 
       
    77 inline TBool CPbkDeleteContactsCmd::MoreContactsToDelete() const
       
    78     {
       
    79     return (iContacts && iContacts->Count() > 0);
       
    80     }
       
    81 
       
    82 inline TInt64 CPbkDeleteContactsCmd::FreeSpaceOnDbDrive() const
       
    83     {
       
    84     TVolumeInfo volInfo;
       
    85     volInfo.iFree = 0;
       
    86     iEngine.FsSession().Volume(volInfo,iDbDrive);
       
    87     return volInfo.iFree;
       
    88     }
       
    89 
       
    90 inline void CPbkDeleteContactsCmd::ConstructL
       
    91         (const CContactIdArray& aContacts)
       
    92     {
       
    93     iContacts = CContactIdArray::NewL(&aContacts);
       
    94     iContacts->ReverseOrder();
       
    95     User::LeaveIfError(iSharedDataClient.Connect());
       
    96     TDriveUnit dbDrive;
       
    97     iEngine.Database().DatabaseDrive(dbDrive);
       
    98     iDbDrive = dbDrive;
       
    99     }
       
   100 
       
   101 CPbkDeleteContactsCmd* CPbkDeleteContactsCmd::NewL
       
   102         (CPbkContactEngine& aEngine,
       
   103         const CContactIdArray& aContacts,
       
   104         CPbkContactViewListControl& aUiControl)
       
   105     {
       
   106     CPbkDeleteContactsCmd* self = new(ELeave)
       
   107         CPbkDeleteContactsCmd(aEngine, aUiControl);
       
   108     CleanupStack::PushL(self);
       
   109     self->ConstructL(aContacts);
       
   110     CleanupStack::Pop(self);
       
   111     return self;
       
   112     }
       
   113 
       
   114 void CPbkDeleteContactsCmd::ResetWhenDestroyed
       
   115         (CPbkDeleteContactsCmd** aSelfPtr)
       
   116     {
       
   117     __ASSERT_DEBUG(!aSelfPtr || *aSelfPtr==this,
       
   118         Panic(EPanicPreCond_ResetWhenDestroyed));
       
   119 
       
   120     iSelfPtr = aSelfPtr;
       
   121     }
       
   122 
       
   123 CPbkDeleteContactsCmd::~CPbkDeleteContactsCmd()
       
   124     {
       
   125     PBK_DEBUG_PRINT
       
   126         (PBK_DEBUG_STRING("CPbkDeleteContactsCmd::~CPbkDeleteContactsCmd(0x%x)"),
       
   127         this);
       
   128 
       
   129     Cancel();
       
   130     if (static_cast<CPbkAppUi*>(CEikonEnv::Static()->AppUi())->
       
   131         IsRunningForeground())
       
   132         {
       
   133         // Unblank and redraw UI control if still visible
       
   134         iUiControl.SetBlank(EFalse);
       
   135         }
       
   136 
       
   137     DeleteProgressNote();
       
   138     iSharedDataClient.Close();
       
   139     delete iContacts;
       
   140 
       
   141     if (iSelfPtr)
       
   142         {
       
   143         *iSelfPtr = NULL;
       
   144         }
       
   145     iUnderDestruction = ETrue;
       
   146     }
       
   147 
       
   148 void CPbkDeleteContactsCmd::ExecuteLD()
       
   149     {
       
   150     PBK_DEBUG_PRINT
       
   151         (PBK_DEBUG_STRING("CPbkDeleteContactsCmd::ExecuteLD(0x%x)"),
       
   152         this);
       
   153     CleanupStack::PushL(this);
       
   154 
       
   155     // Calculate new focus
       
   156     iFocusId = KNullContactId;
       
   157     const TInt focusIndex = iUiControl.NextUnmarkedIndexFromFocus();
       
   158     if (focusIndex >= 0)
       
   159         {
       
   160         iFocusId = iUiControl.ContactIdAtL(focusIndex);
       
   161         }
       
   162 
       
   163     // Blank UI control to avoid flicker
       
   164     iUiControl.SetBlank(ETrue);
       
   165 
       
   166     // delete previous note
       
   167     DeleteProgressNote();
       
   168 
       
   169     // display progress note
       
   170     CAknProgressDialog* progressDialog = new(ELeave) CAknProgressDialog(
       
   171             reinterpret_cast<CEikDialog**>(NULL), ETrue);
       
   172     progressDialog->PrepareLC(R_QTN_PHOB_NOTE_CLEARING_PB);
       
   173     iProgressDlgInfo = progressDialog->GetProgressInfoL();
       
   174     iProgressDlgInfo->SetFinalValue(iContacts->Count());
       
   175     progressDialog->SetCallback(this);
       
   176     iDialogDismissed = EFalse;
       
   177     progressDialog->RunLD();
       
   178 
       
   179     iProgressDialog = progressDialog;
       
   180 
       
   181     // issue request for entry deletion
       
   182     IssueRequest();
       
   183 
       
   184     CleanupStack::Pop(this);
       
   185     }
       
   186 
       
   187 void CPbkDeleteContactsCmd::AddObserver
       
   188         (MPbkCommandObserver& aObserver)
       
   189     {
       
   190     iCommandObserver = &aObserver;
       
   191     }
       
   192 
       
   193 void CPbkDeleteContactsCmd::DoCancel()
       
   194     {
       
   195     delete iContacts;
       
   196     iContacts = NULL;
       
   197     }
       
   198 
       
   199 void CPbkDeleteContactsCmd::RunL()
       
   200     {
       
   201     PBK_DEBUG_PRINT
       
   202         (PBK_DEBUG_STRING("CPbkDeleteContactsCmd::StepL()"),
       
   203         this);
       
   204 
       
   205     CContactDatabase& db = iEngine.Database();
       
   206     CompressIfRequired();
       
   207     if (MoreContactsToDelete())
       
   208         {
       
   209         const TInt sizeBefore = db.FileSize();        
       
   210         iSharedDataClient.RequestFreeDiskSpaceLC(iFreeSpaceRequiredToDelete);        
       
   211         const TInt index = iContacts->Count() - 1;
       
   212         const TContactItemId id = (*iContacts)[index];
       
   213         iContacts->Remove(index);
       
   214 
       
   215         db.DeleteContactL(id);
       
   216         ++iDeletedCount;
       
   217         CleanupStack::PopAndDestroy();  // RequestFreeDiskSpaceLC
       
   218 
       
   219         // Calculate how much database grew rounded to next kB
       
   220         TInt sizeDiff = db.FileSize() - sizeBefore + KB;
       
   221         sizeDiff -= sizeDiff % KB;
       
   222         if (sizeDiff > iFreeSpaceRequiredToDelete)
       
   223             {
       
   224             // Update maximum size required for deletion
       
   225             iFreeSpaceRequiredToDelete = sizeDiff;
       
   226             }
       
   227 
       
   228         // Incrementing progress of the process
       
   229         iProgressDlgInfo->SetAndDraw(iDeletedCount);
       
   230 
       
   231         // issue request to delete next item
       
   232         IssueRequest();
       
   233         }
       
   234     else
       
   235         {
       
   236         // Deletion process completed, all items have been deleted
       
   237         // show result and delete progress note
       
   238         PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkDeleteContactsCmd::RunL process completed start"));
       
   239 
       
   240         // process is completed, all entries have been copied
       
   241         FinishProcess();
       
   242         PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkDeleteContactsCmd::RunL process completed end"));
       
   243         }
       
   244 
       
   245     PBK_DEBUG_PRINT
       
   246         (PBK_DEBUG_STRING("CPbkDeleteContactsCmd::RunL end"));
       
   247     }
       
   248 
       
   249 TInt CPbkDeleteContactsCmd::RunError
       
   250         (TInt aError)
       
   251     {
       
   252     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkDeleteContactsCmd::HandleStepError(%d)"), aError);
       
   253 
       
   254     TInt result = aError;
       
   255     switch (aError)
       
   256         {
       
   257         case KErrNotFound:  // FALLTHROUGH
       
   258         case KErrInUse:     // FALLTHROUGH
       
   259             {
       
   260 			// Ignore these errors
       
   261 			// KErrNotFound means that somebody got the contact first
       
   262 			// KErrInUse means that the contact is open
       
   263             result = KErrNone;
       
   264             break;
       
   265             }
       
   266 
       
   267         default:  // Something more serious happened -> give up
       
   268             {
       
   269             Cancel();
       
   270             FinishProcess();
       
   271             break;
       
   272             }
       
   273         }
       
   274 
       
   275     return result;
       
   276     }
       
   277 
       
   278 void CPbkDeleteContactsCmd::FinishProcess()
       
   279     {
       
   280     PBK_DEBUG_PRINT
       
   281         (PBK_DEBUG_STRING("CPbkDeleteContactsCmd::FinishProcess(0x%x)"),
       
   282         this);
       
   283 
       
   284     // Final compress
       
   285     CompressIfRequired();
       
   286 
       
   287     iUiControl.HandleMarkableListUpdateAfterCommandExecution();
       
   288 
       
   289     // Set the new focus
       
   290     if (iFocusId != KNullContactId)
       
   291         {
       
   292         TInt index = -1;
       
   293         TRAP_IGNORE(index = iUiControl.FindContactIdL(iFocusId));
       
   294         if (index >= 0)
       
   295             {
       
   296             iUiControl.SetCurrentItemIndex(index);
       
   297             }
       
   298         }
       
   299 
       
   300     // Unblank and redraw UI control
       
   301     iUiControl.SetBlank(EFalse);
       
   302 
       
   303     DeleteProgressNote();
       
   304     }
       
   305 
       
   306 void CPbkDeleteContactsCmd::DeleteProgressNote()
       
   307     {
       
   308     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkDeleteContactsCmd::DeleteProgressNote start"));
       
   309 
       
   310     if (iProgressDialog && !iDialogDismissed)
       
   311         {
       
   312         // deletes the dialog
       
   313         TRAPD(err, iProgressDialog->ProcessFinishedL());
       
   314         if (err != KErrNone)
       
   315             {
       
   316             delete iProgressDialog;
       
   317             }
       
   318         iProgressDialog = NULL;
       
   319         }
       
   320 
       
   321     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkDeleteContactsCmd::DeleteProgressNote end"));
       
   322     }
       
   323 
       
   324 void CPbkDeleteContactsCmd::IssueRequest()
       
   325     {
       
   326     TRequestStatus* status = &iStatus;
       
   327     User::RequestComplete(status, KErrNone);
       
   328     SetActive();
       
   329     }
       
   330 
       
   331 void CPbkDeleteContactsCmd::CompressIfRequired()
       
   332     {
       
   333     // Cancel any async compress first
       
   334     iEngine.CancelCompress();
       
   335     CContactDatabase& db = iEngine.Database();
       
   336     // Compress synchronously always if compression is required by the DB
       
   337     // or file system free space is below what is needed for deletion
       
   338     if (db.CompressRequired() ||
       
   339         FreeSpaceOnDbDrive() < iFreeSpaceRequiredToDelete)
       
   340         {
       
   341         TRAP_IGNORE(db.CompactL());
       
   342         }
       
   343     }
       
   344 
       
   345 void CPbkDeleteContactsCmd::DialogDismissedL(TInt /*aButtonId*/)
       
   346     {
       
   347     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkDeleteContactsCmd::DialogDismissedL"));
       
   348     
       
   349     // Cancel deleting process if it is still ongoing
       
   350     Cancel();
       
   351     iDialogDismissed = ETrue;
       
   352     
       
   353     if (static_cast<CPbkAppUi*>(CEikonEnv::Static()->AppUi())->
       
   354         IsRunningForeground())
       
   355         {
       
   356         // Unblank and redraw UI control if still visible
       
   357         iUiControl.SetBlank(EFalse);
       
   358         }
       
   359         
       
   360     // notify command owner that the command has finished
       
   361     iCommandObserver->CommandFinished(*this);
       
   362     }
       
   363 
       
   364 //  End of File