phonebookui/Phonebook/PbkAiwProviders/src/CPbkAssignRingingToneCmd.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 ringing tone functionality.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbkAssignRingingToneCmd.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 <MPbkCommandObserver.h>
       
    32 #include <CPbkRingingToneFetch.h>
       
    33 #include <AiwCommon.h>
       
    34 #include <AiwCommon.hrh>
       
    35 #include <CPbkFFSCheck.h>
       
    36 
       
    37 #include <pbkdebug.h>
       
    38 
       
    39 
       
    40 /// Unnamed namespace for local definitions
       
    41 namespace {
       
    42 
       
    43 // Utility to check file existence without BAFL
       
    44 inline TBool FileExists( RFs& aFs, const TDesC& aFileName )
       
    45     {
       
    46     TEntry ignore;
       
    47     return (aFs.Entry(aFileName, ignore) == KErrNone);
       
    48     }
       
    49 
       
    50 #ifdef _DEBUG
       
    51 enum TPanicCode
       
    52     {
       
    53     ERunL_InvalidState
       
    54     };
       
    55 
       
    56 void Panic(TPanicCode aReason)
       
    57     {
       
    58     _LIT(KPanicText, "CPbkAssignRingingToneCmd");
       
    59     User::Panic(KPanicText, aReason);
       
    60     }
       
    61 
       
    62 #endif  // _DEBUG
       
    63 }
       
    64 
       
    65 // ================= MEMBER FUNCTIONS =======================
       
    66 
       
    67 CPbkAssignRingingToneCmd* CPbkAssignRingingToneCmd::NewL
       
    68         (const TDesC& aFileName, CContactIdArray* aContacts,
       
    69         CPbkContactEngine& aEngine,
       
    70         const CAiwGenericParamList& aInParamList,
       
    71         MAiwNotifyCallback* aNotifyCallback)
       
    72     {
       
    73     CPbkAssignRingingToneCmd* self = new(ELeave) CPbkAssignRingingToneCmd
       
    74         (aFileName, aContacts, aEngine, aInParamList, aNotifyCallback);
       
    75     CleanupStack::PushL(self);
       
    76     self->ConstructL();
       
    77     CleanupStack::Pop(self);
       
    78     return self;
       
    79     }
       
    80 
       
    81 CPbkAssignRingingToneCmd::CPbkAssignRingingToneCmd
       
    82         (const TDesC& aFileName, CContactIdArray* aContacts,
       
    83         CPbkContactEngine& aEngine,
       
    84         const CAiwGenericParamList& aInParamList,
       
    85         MAiwNotifyCallback* aNotifyCallback):
       
    86         CActive(CActive::EPriorityIdle),
       
    87         iEngine(aEngine), iContacts(aContacts),
       
    88         iFileName(aFileName),
       
    89         iInParamList(aInParamList),
       
    90         iNotifyCallback(aNotifyCallback)
       
    91     {
       
    92     CActiveScheduler::Add(this);
       
    93     }
       
    94 
       
    95 void CPbkAssignRingingToneCmd::ConstructL()
       
    96     {
       
    97     iPbkFFSCheck = CPbkFFSCheck::NewL();
       
    98     }
       
    99 
       
   100 CPbkAssignRingingToneCmd::~CPbkAssignRingingToneCmd()
       
   101     {
       
   102     Cancel();
       
   103     delete iWaitNote;
       
   104     delete iItem;
       
   105     delete iContacts;
       
   106     delete iTitleForNote;
       
   107     delete iPbkFFSCheck;
       
   108     }
       
   109 
       
   110 
       
   111 void CPbkAssignRingingToneCmd::AssignRingingToneL()
       
   112     {
       
   113     TInt count = iContacts->Count();
       
   114 
       
   115     delete iItem;
       
   116     iItem = NULL;
       
   117 
       
   118     // Read the last contact of the array. Array is zerobased,
       
   119     // therefore reduce one
       
   120     iItem = iEngine.OpenContactL((*iContacts)[count-1]);
       
   121 
       
   122     // Remove the last contact from the array
       
   123     iContacts->Remove(count-1);
       
   124 
       
   125     if (ConfirmAssignL(*iItem))
       
   126         {
       
   127         SetRingingToneL();
       
   128         IssueRequest();
       
   129         ShowWaitNoteL();
       
   130         }
       
   131     else
       
   132         {
       
   133         // Close contact and continue
       
   134         iEngine.CloseContactL(iItem->Id());
       
   135         IssueRequest();
       
   136         }
       
   137     }
       
   138 
       
   139 /**
       
   140  * Returns ETrue, if it's ok to assign.
       
   141  */
       
   142 inline TBool CPbkAssignRingingToneCmd::ConfirmAssignL(CPbkContactItem& aItem)
       
   143     {
       
   144     TBool ret = ETrue;
       
   145 
       
   146     // Check does the contact already have a ringing tone
       
   147     TPbkContactItemField* field =
       
   148         aItem.FindField(EPbkFieldIdPersonalRingingToneIndication);
       
   149 
       
   150     if (field && (field->StorageType() == KStorageTypeText))
       
   151         {
       
   152         // Check if the existing ringing tone file actually exists
       
   153         if ( FileExists(iEngine.FsSession(), field->Text()) )
       
   154             {
       
   155             DismissWaitNoteL();
       
   156 
       
   157             // Ask the user for confirmation
       
   158             HBufC* title = aItem.GetContactTitleL();
       
   159             CleanupStack::PushL(title);
       
   160             HBufC* prompt = StringLoader::LoadL(
       
   161                 R_QTN_MG_CONTACT_RINGINGTONE_REPLACE, *title);
       
   162             CleanupStack::PopAndDestroy(title);
       
   163             CleanupStack::PushL(prompt);
       
   164             CAknQueryDialog* dlg = CAknQueryDialog::NewL();
       
   165             CleanupStack::PushL(dlg);
       
   166             dlg->SetPromptL(*prompt);
       
   167             CleanupStack::Pop(); // dlg
       
   168 
       
   169             if(!dlg->ExecuteLD(R_PBKAIW_GENERAL_CONFIRMATION_QUERY))
       
   170                 {
       
   171                 ret = EFalse;
       
   172                 }
       
   173             CleanupStack::PopAndDestroy(prompt);
       
   174             }
       
   175         }
       
   176 
       
   177     return ret;
       
   178     }
       
   179 
       
   180 void CPbkAssignRingingToneCmd::SetRingingToneL()
       
   181     {
       
   182 
       
   183     // Check disk space before assigning.
       
   184     // If not enough memory then leaves with KErrDiskFull.
       
   185     iPbkFFSCheck->FFSClCheckL();
       
   186 
       
   187     CPbkRingingToneFetch* toneFetch = CPbkRingingToneFetch::NewL(iEngine);
       
   188     CleanupStack::PushL(toneFetch);
       
   189     toneFetch->SetRingingToneL(*iItem, iFileName);
       
   190     iEngine.CommitContactL(*iItem);
       
   191     CleanupStack::PopAndDestroy(toneFetch);
       
   192 
       
   193 
       
   194     // The title must be fetched and save here otherwise the contact
       
   195     // title displayed in ShowEndNotesL function may be wrong.
       
   196     HBufC* title = iItem->GetContactTitleL();
       
   197     delete iTitleForNote;
       
   198     iTitleForNote = title;
       
   199 
       
   200     ++iTonesAdded;
       
   201     }
       
   202 
       
   203 void CPbkAssignRingingToneCmd::ShowEndNotesL()
       
   204     {
       
   205     DismissWaitNoteL();
       
   206 
       
   207     if (iTonesAdded > 0)
       
   208         {
       
   209         HBufC* prompt = NULL;
       
   210 
       
   211         // Select prompt
       
   212         if (iTonesAdded == 1)
       
   213             {
       
   214             prompt = StringLoader::LoadL(
       
   215                 R_QTN_MG_CONTACT_RINGINGTONE_ADD_ONE, *iTitleForNote);
       
   216             CleanupStack::PushL(prompt);
       
   217             }
       
   218         else if (iTonesAdded > 1)
       
   219             {
       
   220             prompt = StringLoader::LoadLC(
       
   221                 R_QTN_MG_CONTACT_RINGINGTONE_ADD_SEVERAL, iTonesAdded);
       
   222             }
       
   223 
       
   224         // Prepare the note
       
   225         CAknNoteWrapper * note = new (ELeave) CAknNoteWrapper;
       
   226         CleanupStack::PushL(note);
       
   227         note->SetTextL(*prompt);
       
   228         CleanupStack::Pop(note);
       
   229         CleanupStack::PopAndDestroy(prompt);
       
   230 
       
   231         // Show the note
       
   232         note->ExecuteLD(R_PBKAIW_GENERAL_NOTE);
       
   233         }
       
   234 
       
   235     // Destroy itself (observer performs deletion of this command)
       
   236     iCmdObserver->CommandFinished(*this);
       
   237     DoSendNotifyEventL(KAiwEventCompleted);
       
   238     }
       
   239 
       
   240 
       
   241 void CPbkAssignRingingToneCmd::ShowWaitNoteL()
       
   242     {
       
   243     if (!iWaitNote)
       
   244         {
       
   245         CAknWaitDialog* waitNote = new(ELeave)
       
   246         CAknWaitDialog(reinterpret_cast<CEikDialog**>(&iWaitNote));
       
   247         waitNote->ExecuteLD(R_QTN_GEN_NOTE_PROCESSING);
       
   248         iWaitNote = waitNote;
       
   249         }
       
   250     }
       
   251 
       
   252 void CPbkAssignRingingToneCmd::DismissWaitNoteL()
       
   253     {
       
   254     if (iWaitNote)
       
   255         {
       
   256         TRAPD(err, iWaitNote->ProcessFinishedL());
       
   257         if (err != KErrNone)
       
   258             {
       
   259             delete iWaitNote;
       
   260             iWaitNote = NULL;
       
   261             }
       
   262         }
       
   263     }
       
   264 
       
   265 void CPbkAssignRingingToneCmd::ExecuteLD()
       
   266     {
       
   267     iState = EAssigning;
       
   268     IssueRequest();
       
   269     }
       
   270 
       
   271 void CPbkAssignRingingToneCmd::AddObserver(MPbkCommandObserver& aObserver)
       
   272     {
       
   273     iCmdObserver = &aObserver;
       
   274     }
       
   275 
       
   276 void CPbkAssignRingingToneCmd::RunL()
       
   277     {
       
   278     switch (iState)
       
   279         {
       
   280         case EAssigning:
       
   281             {
       
   282             if (iContacts->Count() > 0)
       
   283                 {
       
   284                 // Assign ringing tone to next contact
       
   285                 AssignRingingToneL();
       
   286                 iState = EAssigning;
       
   287                 }
       
   288             else
       
   289                 {
       
   290                 IssueStopRequest(KErrNone);
       
   291                 }
       
   292             break;
       
   293             }
       
   294         case EStopping:
       
   295             {
       
   296             // Show notes to user
       
   297             ShowEndNotesL();
       
   298             break;
       
   299             }
       
   300         default:
       
   301             {
       
   302             __ASSERT_DEBUG(EFalse, Panic(ERunL_InvalidState));
       
   303             break;
       
   304             }
       
   305         }
       
   306     }
       
   307 
       
   308 
       
   309 void CPbkAssignRingingToneCmd::DoCancel()
       
   310     {
       
   311     // Safe to ignore this, notify is not so critical
       
   312     TRAP_IGNORE(DoSendNotifyEventL(KAiwEventCanceled));
       
   313     }
       
   314 
       
   315 TInt CPbkAssignRingingToneCmd::RunError(TInt aError)
       
   316     {
       
   317     if (iItem)
       
   318         {
       
   319         // We can ignore the error since stopping anyways
       
   320         TRAP_IGNORE(iEngine.CloseContactL(iItem->Id()));
       
   321         }
       
   322     CCoeEnv::Static()->HandleError(aError);
       
   323     IssueStopRequest(aError);
       
   324     return KErrNone;
       
   325     }
       
   326 
       
   327 void CPbkAssignRingingToneCmd::DoSendNotifyEventL(TInt aEvent)
       
   328     {
       
   329     if (iNotifyCallback)
       
   330         {
       
   331         CAiwGenericParamList* eventParamList = CAiwGenericParamList::NewL();
       
   332         CleanupStack::PushL(eventParamList);
       
   333         iNotifyCallback->HandleNotifyL(
       
   334             KAiwCmdAssign,
       
   335             aEvent,
       
   336             *eventParamList,
       
   337             iInParamList);
       
   338         CleanupStack::PopAndDestroy(eventParamList);
       
   339         }
       
   340     }
       
   341 
       
   342 void CPbkAssignRingingToneCmd::IssueRequest()
       
   343     {
       
   344     TRequestStatus* status = &iStatus;
       
   345     User::RequestComplete(status, KErrNone);
       
   346     SetActive();
       
   347     }
       
   348 
       
   349 void CPbkAssignRingingToneCmd::IssueStopRequest(TInt /*aError*/)
       
   350     {
       
   351     iState = EStopping;
       
   352     if (!IsActive())
       
   353         {
       
   354         IssueRequest();
       
   355         }
       
   356     }
       
   357 
       
   358 
       
   359 // End of File
       
   360