# examples/ForumNokia/S60_3rd_Edition_TextMTM_Example/modules/UI/src/txtmtmcreateop.cpp

    00001 //
    00002 // © 2004 Nokia Corporation.  All rights reserved.
    00003 //
    00004
    00005 // class include
    00006 #include "txtmtmcreateop.h"
    00007
    00008 //system include
    00009 #include <apparc.h>    // CApaApplication
    00010 #include <msvids.h>    // KMsvLocalServiceIndexEntryId
    00011 #include <msvuids.h>   // KUidMsvMessageEntry
    00012 #include <ecom.h>
    00013 #include <txtrich.h>
    00014 // user include
    00015 #include "txtupan.h"
    00016 #include "txtu.h"
    00017
    00018 #include "TXTCMDS.HRH"
    00019
    00020
    00021
    00022 // constants
    00023 _LIT(KTxtGeneratedFilename, "C:\\data\\TextMTMService\\sentmsgbody.txt"); // generated filename base
    00024 _LIT(KDefaultMtmName, "My TextMTM");
    00025 CTxtMtmCreateOperation* CTxtMtmCreateOperation::NewL(const TMsvEntry& aEntry, CMsvEntry& aParent, CMsvSession& aMsvSession, CBaseMtmUi& aMtmUi, TRequestStatus& aObserverRequestStatus)
    00026     {
    00027     CTxtMtmCreateOperation* self = new (ELeave) CTxtMtmCreateOperation(aEntry, aParent, aMsvSession, aMtmUi, aObserverRequestStatus);
    00028     CleanupStack::PushL(self);
    00029     self->ConstructL();
    00030     CleanupStack::Pop(self);
    00031     return self;
    00032     }
    00033
    00034 CTxtMtmCreateOperation::CTxtMtmCreateOperation(const TMsvEntry& aEntry, CMsvEntry& aParent, CMsvSession& aMsvSession, CBaseMtmUi& aMtmUi, TRequestStatus& aObserverRequestStatus)
    00035  :  CMsvOperation(aMsvSession, EPriorityStandard, aObserverRequestStatus),
    00036     iEntry(aEntry),
    00037     iParent(aParent),
    00038     iMtmUi(aMtmUi)
    00039     {
    00040     CActiveScheduler::Add(this);
    00041     }
    00042
    00043 void CTxtMtmCreateOperation::ConstructL()
    00044     {
    00045     // set the obeserver request status as pending so that the observer
    00046     // won't complete
    00047     iObserverRequestStatus = KRequestPending;
    00048
    00049     Schedule(KErrNone);
    00050     }
    00051
    00052 CTxtMtmCreateOperation::~CTxtMtmCreateOperation()
    00053     {
    00054     Cancel();
    00055     delete iOp;
    00056     }
    00057
    00058 const TDesC8& CTxtMtmCreateOperation::ProgressL()
    00059     {
    00060     if(!IsActive())
    00061         User::Leave(KErrNotReady);
    00062     iProgressBuf() = iProgress;
    00063     return iProgressBuf;
    00064     }
    00065
    00066 const TDesC8& CTxtMtmCreateOperation::FinalProgress()
    00067     {
    00068     __ASSERT_ALWAYS(!IsActive(), User::Panic(KTEXTMTMUIPanic, ETextMtmUiOperationActive));
    00069     iProgressBuf() = iProgress;
    00070     return iProgressBuf;
    00071     }
    00072
    00073 TInt CTxtMtmCreateOperation::RunError(TInt aError)
    00074     {
    00075     if(iProgress.iState > TTxtMtmCreateOpProgress::ECreateMessage)
    00076         iMsvSession.RemoveEntry(iProgress.iFinalMsgId);
    00077
    00078     // Populate the progress structure with the error code
    00079     iProgress.iError = aError;
    00080     // Complete the requesting active object with the error code
    00081     TRequestStatus* stat = &iObserverRequestStatus;
    00082     User::RequestComplete(stat, aError);
    00083     // Returning anything other than KErrNone from this method results in a panic
    00084     return KErrNone;
    00085     }
    00086
    00087 void CTxtMtmCreateOperation::DoCancel()
    00088     {
    00089     if(iOp)
    00090         iOp->Cancel();
    00091
    00092     if(iProgress.iState > TTxtMtmCreateOpProgress::ECreateMessage)
    00093         iMsvSession.RemoveEntry(iProgress.iFinalMsgId);
    00094
    00095     TRequestStatus* stat = &iObserverRequestStatus;
    00096     User::RequestComplete(stat, KErrCancel);
    00097     }
    00098
    00099 void CTxtMtmCreateOperation::RunL()
    00100     {
    00101     User::LeaveIfError(iStatus.Int());
    00102
    00103     switch(iProgress.iState)
    00104         {
    00105         case TTxtMtmCreateOpProgress::EInit:
    00106             iProgress.iState = TTxtMtmCreateOpProgress::ECreateMessage;
    00107             CreateMessageL();  // reschedules the active object
    00108             break;
    00109
    00110         case TTxtMtmCreateOpProgress::ECreateMessage:
    00111             iProgress.iState = TTxtMtmCreateOpProgress::EEditMessage;
    00112             ScheduleEditL();  // reschedules the active object
    00113             break;
    00114
    00115         case TTxtMtmCreateOpProgress::EEditMessage:
    00116             iProgress.iState = TTxtMtmCreateOpProgress::EFinished;
    00117
    00118
    00119         default:
    00120             break;
    00121         }
    00122
    00123     // When the state machine has been completed, this active object won't
    00124     // be scheduled to complete a request
    00125     if(!IsActive())
    00126         {
    00127         TRequestStatus* stat = &iObserverRequestStatus;
    00128         User::RequestComplete(stat, KErrNone);
    00129         }
    00130     }
    00131
    00132
    00133 void CTxtMtmCreateOperation::CreateMessageL()
    00134     {
    00135
    00136
    00137         TFileName filename = KTxtGeneratedFilename();
    00138     if( iEntry.iType == KUidMsvMessageEntry)
    00139         {
    00140         _LIT(KFilename,"message.txt");
    00141             iEntry.iDescription.Set(KFilename);
    00142             iEntry.iDate.UniversalTime();
    00143         iEntry.iServiceId=KMsvLocalServiceIndexEntryId;
    00144         iParent.SetEntryL(KMsvDraftEntryId);
    00145         iParent.CreateL(iEntry);
    00146                 iProgress.iFinalMsgId = iEntry.Id();
    00147                 }
    00148     else
    00149         {
    00150         if (iEntry.iType ==KUidMsvServiceEntry)
    00151                 {
    00152                 iEntry.iDetails.Set(KDefaultMtmName);
    00153                 iParent.SetEntryL(KMsvRootIndexEntryId);
    00154                 iParent.CreateL(iEntry);
    00155                 iProgress.iFinalMsgId = iEntry.Id();
    00156
    00157                 // Set the root folder setting
    00158             _LIT(KRoot, "C:\\data\\TextMTMService\\");
    00159                 TMTMTxtSettings set;
    00160                         CMTMTxtSettings* settings = CMTMTxtSettings::NewL();
    00161                         set.SetRootFolder(KRoot);
    00162                         CleanupStack::PushL(settings);
    00163                         settings->SaveSettingsL(iEntry.Id(), set);
    00164                         CleanupStack::PopAndDestroy(settings);
    00165                         }
    00166         }
    00167         Schedule(KErrNone);
    00168     }
    00169
    00170 void CTxtMtmCreateOperation::ScheduleEditL()
    00171     {
    00172     iMtmUi.BaseMtm().SwitchCurrentEntryL(iProgress.iFinalMsgId);
    00173
    00174     delete iOp;
    00175     iOp = NULL;
    00176
    00177     // The Edit method returns a CMsvOperation that will complete this class's request status
    00178     // when editing is complete, resulting in this class's RunL method being called by the active scheduler
    00179     SetActive();
    00180
    00181     iOp = iMtmUi.EditL(iStatus);
    00182     }
    00183
    00184 void CTxtMtmCreateOperation::Schedule(TInt aError)
    00185     {
    00186     // Schedules the transaction so that the RunL method of this active
    00187     // object will get called again
    00188     SetActive();
    00189     TRequestStatus* stat = &iStatus;
    00190     User::RequestComplete(stat, aError);
    00191     }
    00192