phonebookui/Phonebook/PbkAiwProviders/src/CPbkAssignImageCmd.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 *     Implements assign image functionality.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbkAssignImageCmd.h"
       
    22 
       
    23 #include <aknnotedialog.h>
       
    24 #include <StringLoader.h>
       
    25 #include <PbkAiwProvidersRes.rsg>
       
    26 #include <PbkView.rsg>
       
    27 #include <CPbkContactEngine.h>
       
    28 #include <AknNoteWrappers.h>
       
    29 #include <AknWaitDialog.h>
       
    30 #include <CPbkContactItem.h>
       
    31 #include <CPbkThumbnailManager.h>
       
    32 #include <CPbkDrmManager.h>
       
    33 #include <MPbkCommandObserver.h>
       
    34 #include <AiwCommon.h>
       
    35 #include <AiwCommon.hrh>
       
    36 #include <CPbkFFSCheck.h>
       
    37 
       
    38 #include <pbkdebug.h>
       
    39 
       
    40 
       
    41 /// Unnamed namespace for local definitions
       
    42 namespace {
       
    43 
       
    44 
       
    45 #ifdef _DEBUG
       
    46 enum TPanicCode
       
    47     {
       
    48     EPanicPreCond_PbkImageSetComplete,
       
    49     EPanicPreCond_PbkImageSetFailed,
       
    50     EPanicPreCond_PbkImageGetComplete,
       
    51     EPanicPreCond_PbkImageGetFailed,
       
    52     ERunL_InvalidState
       
    53     };
       
    54 
       
    55 void Panic(TPanicCode aReason)
       
    56     {
       
    57     _LIT(KPanicText, "CPbkAssignImageCmd");
       
    58     User::Panic(KPanicText, aReason);
       
    59     }
       
    60 
       
    61 #endif  // _DEBUG
       
    62 }
       
    63 
       
    64 // ================= MEMBER FUNCTIONS =======================
       
    65 
       
    66 CPbkAssignImageCmd* CPbkAssignImageCmd::NewL
       
    67         (const TDesC& aFileName, CContactIdArray* aContacts,
       
    68         TPbkFieldId aFieldId, CPbkContactEngine& aEngine,
       
    69         const CAiwGenericParamList& aInParamList,
       
    70         MAiwNotifyCallback* aNotifyCallback)
       
    71     {
       
    72 	CPbkAssignImageCmd* self = new(ELeave) CPbkAssignImageCmd
       
    73         (aFileName, aContacts, aFieldId, aEngine,
       
    74         aInParamList, aNotifyCallback);
       
    75 	CleanupStack::PushL(self);
       
    76 	self->ConstructL();
       
    77 	CleanupStack::Pop(self);
       
    78 	return self;
       
    79     }
       
    80 
       
    81 CPbkAssignImageCmd::CPbkAssignImageCmd
       
    82         (const TDesC& aFileName, CContactIdArray* aContacts,
       
    83         TPbkFieldId aFieldId, CPbkContactEngine& aEngine,
       
    84         const CAiwGenericParamList& aInParamList,
       
    85         MAiwNotifyCallback* aNotifyCallback) :
       
    86         CActive(CActive::EPriorityIdle),
       
    87         iEngine(aEngine), iContacts(aContacts),
       
    88         iFieldId(aFieldId), iFileName(aFileName),
       
    89         iInParamList(aInParamList),
       
    90         iNotifyCallback(aNotifyCallback)
       
    91     {
       
    92     CActiveScheduler::Add(this);
       
    93     }
       
    94 
       
    95 void CPbkAssignImageCmd::ConstructL()
       
    96     {
       
    97     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
    98         ("CPbkAssignImageCmd::ConstructL start"));
       
    99 
       
   100     iImageManager = CPbkThumbnailManager::NewL(iEngine);
       
   101     iPbkFFSCheck = CPbkFFSCheck::NewL();
       
   102     
       
   103     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   104         ("CPbkAssignImageCmd::ConstructL end"));
       
   105     }
       
   106 
       
   107 CPbkAssignImageCmd::~CPbkAssignImageCmd()
       
   108     {
       
   109     Cancel();
       
   110     delete iWaitNote;
       
   111     delete iItem;
       
   112     delete iImageOperation;
       
   113     delete iImageManager;
       
   114     delete iContacts;
       
   115     delete iBitmap;
       
   116     delete iTitleForNote;
       
   117     delete iPbkFFSCheck;
       
   118     }
       
   119 
       
   120 
       
   121 void CPbkAssignImageCmd::AssignImageL()
       
   122     {    
       
   123     TInt count = iContacts->Count();
       
   124 
       
   125     delete iItem;
       
   126     iItem = NULL;
       
   127     
       
   128     delete iBitmap;
       
   129     iBitmap = NULL;
       
   130 
       
   131     // Read the last contact of the array. Array is zerobased,
       
   132     // therefore reduce one
       
   133     iItem = iEngine.OpenContactL((*iContacts)[count-1]);
       
   134     
       
   135     // Remove the last contact from the array
       
   136     iContacts->Remove(count-1);
       
   137 
       
   138 	if (ConfirmAssignL(*iItem, iFieldId))
       
   139 		{		
       
   140         SetImageAsyncL();
       
   141         ShowWaitNoteL();
       
   142 		}
       
   143 	else
       
   144 		{
       
   145         // Close contact and continue
       
   146         iEngine.CloseContactL(iItem->Id());
       
   147         IssueRequest();
       
   148 		}
       
   149     }
       
   150     
       
   151 inline TBool CPbkAssignImageCmd::ConfirmAssignL
       
   152         (CPbkContactItem& aItem, TPbkFieldId /*aId*/)
       
   153 	{
       
   154 	TBool ret = ETrue;
       
   155 	
       
   156     // Check does the contact already have a image
       
   157     if (iImageManager->HasThumbnail(aItem))
       
   158         {
       
   159         DismissWaitNoteL();
       
   160 
       
   161         // Ask the user for confirmation
       
   162         HBufC* title = aItem.GetContactTitleL();
       
   163         CleanupStack::PushL(title);
       
   164         HBufC* prompt = StringLoader::LoadL(R_QTN_PHOB_FETCH_IMG_REPLACE,
       
   165             *title);
       
   166         CleanupStack::PopAndDestroy(title);
       
   167         CleanupStack::PushL(prompt);
       
   168         CAknQueryDialog* dlg = CAknQueryDialog::NewL();
       
   169 		CleanupStack::PushL(dlg);
       
   170         dlg->SetPromptL(*prompt);
       
   171 		CleanupStack::Pop(); // dlg
       
   172 
       
   173         if(!dlg->ExecuteLD(R_PBKAIW_GENERAL_CONFIRMATION_QUERY))
       
   174             {
       
   175             ret = EFalse;
       
   176             }
       
   177         CleanupStack::PopAndDestroy(prompt);	
       
   178         }
       
   179 	
       
   180 	return ret;
       
   181 	}
       
   182 
       
   183 void CPbkAssignImageCmd::SetImageAsyncL()
       
   184     {    
       
   185     // Set image to contact
       
   186     delete iImageOperation;
       
   187     iImageOperation = NULL;
       
   188     
       
   189     // Check disk space before assigning. 
       
   190     // If not enough memory then leaves with KErrDiskFull.
       
   191     iPbkFFSCheck->FFSClCheckL();
       
   192     
       
   193     if (!iBitmap)
       
   194         {
       
   195         iImageOperation = iImageManager->SetThumbnailAsyncL
       
   196             (*this, *iItem, iFileName);
       
   197         }
       
   198     else
       
   199         {
       
   200         iImageOperation = iImageManager->SetThumbnailAsyncL
       
   201             (*this, *iItem, *iBitmap);
       
   202         }
       
   203     }
       
   204 
       
   205 void CPbkAssignImageCmd::ShowEndNotesL()
       
   206     {
       
   207     DismissWaitNoteL();
       
   208 
       
   209     if (iImagesAdded > 0)
       
   210         {
       
   211         HBufC* prompt = NULL;
       
   212 
       
   213         // Select prompt
       
   214         if (iImagesAdded == 1)
       
   215             {
       
   216             prompt = StringLoader::LoadL(R_QTN_PHOB_FETCH_NOTE_IMG_ADD_ONE,
       
   217                 *iTitleForNote);
       
   218             CleanupStack::PushL(prompt);
       
   219             }
       
   220         else if (iImagesAdded > 1)
       
   221             {
       
   222             prompt = StringLoader::LoadLC(R_QTN_PHOB_FETCH_NOTE_IMG_ADD_SEV,
       
   223                 iImagesAdded);
       
   224             }
       
   225 
       
   226         // Prepare the note
       
   227         CAknNoteWrapper * note = new (ELeave) CAknNoteWrapper;
       
   228         CleanupStack::PushL(note);
       
   229         note->SetTextL(*prompt);
       
   230         CleanupStack::Pop(note);
       
   231         CleanupStack::PopAndDestroy(prompt);
       
   232 
       
   233         // Show the note
       
   234         note->ExecuteLD(R_PBKAIW_GENERAL_NOTE);
       
   235         }
       
   236 
       
   237     // Destroy itself (observer performs deletion of this command)
       
   238     iCmdObserver->CommandFinished(*this);
       
   239     DoSendNotifyEventL(KAiwEventCompleted);
       
   240     }
       
   241 
       
   242 void CPbkAssignImageCmd::FetchImageL()
       
   243     {
       
   244     // If we are to continue the image set process
       
   245     // get the image from the previous item,
       
   246     // so there's no need to perform scale and other
       
   247     // operations for every contact
       
   248     delete iImageOperation;
       
   249     iImageOperation = NULL;
       
   250     iImageOperation = iImageManager->GetThumbnailAsyncL
       
   251         (*this, *iItem);
       
   252     }
       
   253 
       
   254 void CPbkAssignImageCmd::PbkThumbnailSetComplete
       
   255         (MPbkThumbnailOperation& 
       
   256         #ifdef _DEBUG
       
   257         aOperation
       
   258         #endif
       
   259         )
       
   260     {
       
   261     __ASSERT_DEBUG(&aOperation == iImageOperation, 
       
   262         Panic(EPanicPreCond_PbkImageSetComplete));
       
   263 
       
   264     // Commit changes
       
   265     TRAPD(err, iEngine.CommitContactL(*iItem, ETrue));
       
   266     delete iImageOperation;
       
   267     iImageOperation = NULL;        
       
   268     if (err == KErrNone)
       
   269         {
       
   270         // Save contact title for note
       
   271         HBufC* title = NULL;        
       
   272         TRAP(err, title = iItem->GetContactTitleL());        
       
   273         if (err == KErrNone)
       
   274             {
       
   275             delete iTitleForNote;
       
   276             iTitleForNote = title;
       
   277             // Increase the count only if commit and title name
       
   278             // fetch succeeded
       
   279             ++iImagesAdded;    
       
   280             }
       
   281         else
       
   282             {
       
   283             CCoeEnv::Static()->HandleError(err);
       
   284             }        
       
   285         }
       
   286     else
       
   287         {
       
   288         CCoeEnv::Static()->HandleError(err);
       
   289         }
       
   290     
       
   291     // Continue
       
   292     IssueRequest();
       
   293     }
       
   294 
       
   295 void CPbkAssignImageCmd::PbkThumbnailSetFailed
       
   296         (MPbkThumbnailOperation& 
       
   297         #ifdef _DEBUG
       
   298         aOperation
       
   299         #endif
       
   300         , TInt aError)
       
   301     {
       
   302     __ASSERT_DEBUG(&aOperation == iImageOperation, 
       
   303         Panic(EPanicPreCond_PbkImageSetFailed));
       
   304 
       
   305     delete iImageOperation;
       
   306     iImageOperation = NULL;
       
   307 
       
   308     // We can ignore the error, report only the
       
   309     // original error to user
       
   310     TRAP_IGNORE(iEngine.CloseContactL(iItem->Id()));
       
   311 
       
   312     // Report the failure to the user
       
   313     CCoeEnv::Static()->HandleError(aError);
       
   314 
       
   315     // Continue
       
   316     IssueRequest();
       
   317     }
       
   318 
       
   319 
       
   320 void CPbkAssignImageCmd::PbkThumbnailGetComplete
       
   321         (MPbkThumbnailOperation& 
       
   322         #ifdef _DEBUG
       
   323         aOperation
       
   324         #endif
       
   325         , CFbsBitmap* aBitmap)
       
   326     {
       
   327     __ASSERT_DEBUG(&aOperation == iImageOperation, 
       
   328         Panic(EPanicPreCond_PbkImageSetComplete));
       
   329 
       
   330     // Store the bitmap
       
   331     iBitmap = aBitmap;
       
   332 
       
   333     // Continue
       
   334     IssueRequest();
       
   335     }
       
   336 
       
   337 void CPbkAssignImageCmd::PbkThumbnailGetFailed
       
   338         (MPbkThumbnailOperation& 
       
   339         #ifdef _DEBUG
       
   340         aOperation
       
   341         #endif
       
   342         , TInt /*aError*/)
       
   343     {
       
   344     __ASSERT_DEBUG(&aOperation == iImageOperation, 
       
   345         Panic(EPanicPreCond_PbkImageGetFailed));
       
   346 
       
   347     // Continue
       
   348     IssueRequest();
       
   349     }
       
   350 
       
   351 
       
   352 void CPbkAssignImageCmd::ShowWaitNoteL()
       
   353     {
       
   354     if (!iWaitNote)
       
   355         {
       
   356         CAknWaitDialog* waitNote = new(ELeave)
       
   357         CAknWaitDialog(reinterpret_cast<CEikDialog**>(&iWaitNote));
       
   358         waitNote->ExecuteLD(R_QTN_GEN_NOTE_PROCESSING);
       
   359         iWaitNote = waitNote;
       
   360         }
       
   361     }
       
   362 
       
   363 void CPbkAssignImageCmd::DismissWaitNoteL()
       
   364     {
       
   365     if (iWaitNote)
       
   366         {
       
   367 	    TRAPD(err, iWaitNote->ProcessFinishedL());
       
   368 	    if (err != KErrNone)
       
   369 		    {
       
   370 		    delete iWaitNote;
       
   371             iWaitNote = NULL;
       
   372 		    }
       
   373         }
       
   374     }
       
   375 
       
   376 void CPbkAssignImageCmd::ExecuteLD()
       
   377     {
       
   378     CleanupStack::PushL(this);
       
   379     
       
   380     // Check is the image DRM protected
       
   381     CPbkDrmManager* drmManager = CPbkDrmManager::NewL();
       
   382     CleanupStack::PushL(drmManager);
       
   383     if (drmManager->IsProtectedFile(iFileName))
       
   384         {
       
   385         // Show user copyright note
       
   386         HBufC* prompt = 
       
   387             CCoeEnv::Static()->AllocReadResourceLC(R_QTN_DRM_NOT_ALLOWED);
       
   388         CAknInformationNote* dlg = new(ELeave) CAknInformationNote(ETrue);
       
   389         dlg->ExecuteLD(*prompt);
       
   390         CleanupStack::PopAndDestroy(); // prompt
       
   391         IssueStopRequest(KErrCancel);
       
   392         }
       
   393     else
       
   394         {        
       
   395         iState = EAssigningFirst;
       
   396         IssueRequest();
       
   397         }
       
   398     CleanupStack::PopAndDestroy(drmManager);
       
   399     CleanupStack::Pop(this);
       
   400     }
       
   401 
       
   402 void CPbkAssignImageCmd::AddObserver(MPbkCommandObserver& aObserver)
       
   403     {
       
   404     iCmdObserver = &aObserver;
       
   405     }
       
   406 
       
   407 void CPbkAssignImageCmd::RunL()
       
   408     {
       
   409     switch (iState)
       
   410         {
       
   411         case EAssigningFirst:
       
   412             {                        
       
   413             AssignImageL();
       
   414 
       
   415             if (iContacts->Count() > 0)
       
   416                 {
       
   417                 // Fetch the assigned image for the next contacts, if any
       
   418                 iState = EFetching;
       
   419                 }
       
   420             else
       
   421                 {
       
   422                 // Only one contact was selected, stop
       
   423                 iState = EStopping;
       
   424                 }
       
   425             break;
       
   426             }
       
   427         case EFetching:
       
   428             {
       
   429             FetchImageL();
       
   430 
       
   431             iState = EAssigning;
       
   432             break;
       
   433             }
       
   434         case EAssigning:
       
   435             {
       
   436             if (iContacts->Count() > 0)
       
   437                 {
       
   438                 // Assign image to next contact
       
   439                 AssignImageL();
       
   440                 iState = EAssigning;
       
   441                 }
       
   442             else
       
   443                 {
       
   444                 IssueStopRequest(KErrNone);
       
   445                 }
       
   446             break;
       
   447             }
       
   448         case EStopping:
       
   449             {
       
   450             // Show notes to user
       
   451             ShowEndNotesL();
       
   452             break;
       
   453             }
       
   454         default:
       
   455             {
       
   456             __ASSERT_DEBUG(EFalse, Panic(ERunL_InvalidState));
       
   457             break;
       
   458             }
       
   459         }
       
   460     }
       
   461 
       
   462 
       
   463 void CPbkAssignImageCmd::DoCancel()
       
   464     {
       
   465     // Safe to ignore this, notify is not so critical
       
   466     TRAP_IGNORE(DoSendNotifyEventL(KAiwEventCanceled));
       
   467     }
       
   468     
       
   469 
       
   470 TInt CPbkAssignImageCmd::RunError(TInt aError)
       
   471     {
       
   472     if (iItem)
       
   473         {
       
   474         // We can ignore the error since stopping anyways
       
   475         TRAP_IGNORE(iEngine.CloseContactL(iItem->Id()));
       
   476         }
       
   477     CCoeEnv::Static()->HandleError(aError);
       
   478     IssueStopRequest(aError);
       
   479     return KErrNone;
       
   480     }
       
   481 
       
   482 void CPbkAssignImageCmd::DoSendNotifyEventL(TInt aEvent)
       
   483     {
       
   484     if (iNotifyCallback)
       
   485         {
       
   486         CAiwGenericParamList* eventParamList = CAiwGenericParamList::NewL();
       
   487         CleanupStack::PushL(eventParamList);
       
   488         iNotifyCallback->HandleNotifyL(
       
   489 		    KAiwCmdAssign,
       
   490 		    aEvent,
       
   491 		    *eventParamList,
       
   492 		    iInParamList);
       
   493         CleanupStack::PopAndDestroy(eventParamList);
       
   494         }
       
   495     }
       
   496 
       
   497 void CPbkAssignImageCmd::IssueRequest()
       
   498     {
       
   499     TRequestStatus* status = &iStatus;
       
   500     User::RequestComplete(status, KErrNone);
       
   501     SetActive();
       
   502     }
       
   503 
       
   504 void CPbkAssignImageCmd::IssueStopRequest(TInt /*aError*/)
       
   505     {
       
   506     iState = EStopping;
       
   507     if (!IsActive())
       
   508         {
       
   509         IssueRequest();
       
   510         }
       
   511     }
       
   512 
       
   513 // End of File
       
   514