×

Discussion Board

Results 1 to 2 of 2

Hybrid View

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    What is the error in my code here? Unable to Save document to file

    Dear friends.
    hi. I have created a simple editor, and in that the document is opening but not able to save the docuemnt to a file. everytime i save, i get "document saved" message.

    But once i save and create another document, my old document's contents are lost and it displays only an empty file. Also when i try to close the application, i get KERN-EXEC 3 error? I know I am wrong somewhere logically/concept wise since i am a fresher to symbian development.


    Can someone pl go thru the code below and help me rectify these errors and save the document to the file and restore back the file for display.

    ============================
    // chattst.cpp
    //

    #include "chattst.h"

    //
    // EXPORTed functions
    //

    EXPORT_C CApaApplication* NewApplication()
    {
    return new CChattstApplication;
    }
    GLDEF_C TInt E32Dll(TDllReason)
    {
    return KErrNone;
    }

    ////////////////////////////////////////////////////////////////
    //
    // Application class, CChattstApplication
    //
    ////////////////////////////////////////////////////////////////

    TUid CChattstApplication::AppDllUid() const
    {
    return KUidChattst;
    }

    CApaDocument* CChattstApplication::CreateDocumentL()
    {
    // Construct the document using its NewL() function, rather
    // than using new(ELeave), because it requires two-phase
    // construction.
    return CChattstDocument::NewL(*this);
    }





    //
    // CChattstDocument
    //

    // The constructor of the document class just passes the
    // supplied reference to the constructor initialisation list.
    // The document has no real work to do in this application.
    //
    CChattstDocument::CChattstDocument(CEikApplication& aApp)
    : CEikDocument(aApp)
    {

    }

    CChattstDocument* CChattstDocument::NewL(CEikApplication& aApp)
    {
    CChattstDocument* self=new(ELeave) CChattstDocument(aApp);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop();
    return self;
    }


    void CChattstDocument::ConstructL()
    {
    TCharFormatMask defaultCharFormatMask;
    TCharFormat defaultCharFormat(_L("Arial&quot,178); // !! should seed by some other means
    defaultCharFormatMask.SetAttrib(EAttFontTypeface);
    defaultCharFormatMask.SetAttrib(EAttFontHeight);

    iParaFormatLayer=CParaFormatLayer::NewL();
    iCharFormatLayer=CCharFormatLayer::NewL(defaultCharFormat,defaultCharFormatMask);
    iRichTextOutput=CRichText::NewL(iParaFormatLayer,iCharFormatLayer,CEditableText::ESegmentedStorage,5);
    }

    CChattstDocument::~CChattstDocument()
    {
    delete iRichTextOutput;
    delete iCharFormatLayer;
    delete iParaFormatLayer;
    }

    const CStreamStore& CChattstDocument::StreamStoreL(TInt /*aPos*/) const
    {
    return *EditStore();
    }

    void CChattstDocument:oNewFileL(const TFileName& aFileName)
    {
    //CFileStore* store=CDirectFileStore::ReplaceLC(Process()->FsSession(),aFileName,EFileShareExclusive);
    CFileStore* store=CPermanentFileStore::ReplaceLC(Process()->FsSession(),aFileName,EFileShareExclusive);
    TUidType fileUid(KDirectFileStoreLayoutUid,KUidAppDllDoc,Application()->AppDllUid());
    store->SetTypeL(fileUid);
    CStreamDictionary* streamDic=CStreamDictionary::NewL();
    CleanupStack::PushL(streamDic);
    Process()->WriteRootStreamL(*store,*streamDic,*Application());
    store->CommitL();
    CleanupStack::PopAndDestroy(); // streamDic
    NewDocumentL();
    CleanupStack::Pop(); // store
    delete((CEikProcess*)Process())->MainStore(); // remove the [original] store
    ((CEikProcess*)Process())->SetMainStore(store); // set the new file store as the main one.
    Process()->SetMainDocFileName(aFileName);
    SetChanged(EFalse);
    }

    void CChattstDocument:oOpenFileL(const TFileName& aFileName)
    {
    CFileStore* store=NULL;
    CStreamDictionary* dic=CApaProcess::ReadRootStreamLC(Process()->FsSession(),store,aFileName,EFileShareExclusive|EFileWrite);
    CleanupStack::PushL(store);
    RestoreL(*store,*dic);
    CleanupStack::Pop(); // store
    CleanupStack::PopAndDestroy(); // dictionary
    delete((CEikProcess*)Process())->MainStore(); // remove the [original] store
    ((CEikProcess*)Process())->SetMainStore(store); // set the new file store as the main one.
    Process()->SetMainDocFileName(aFileName);
    SetChanged(EFalse);
    }
    // Save model data to the new file. This is done:
    // 1. by constructing a new store of this name
    // 2. closing the current store
    // 3. making the new store the main one
    void CChattstDocument:oFileSaveToNewL(const TFileName& aNewFileName)
    {
    ((CEikProcess*)Process())->SaveToDirectFileStoreL(this,&aNewFileName);
    SetChanged(EFalse);
    }

    // Implement the reversion.
    //
    //
    void CChattstDocument:oFileRevertL()
    {
    CStreamDictionary* streamDic=CStreamDictionary::NewL();
    CleanupStack::PushL(streamDic);
    CFileStore* mainStore=REINTERPRET_CAST(CEikProcess*,Process())->MainStore();
    RStoreReadStream root;
    root.OpenLC(*mainStore,mainStore->Root());
    root>> *streamDic;
    root.Close();
    CleanupStack::PopAndDestroy(); // root
    RestoreL(*mainStore,*streamDic);
    CleanupStack::PopAndDestroy(); // streamDic
    }


    void CChattstDocument::NewDocumentL()
    {
    iRichTextOutput->Reset();
    }

    void CChattstDocument::RestoreL(const CStreamStore& aStore,const CStreamDictionary& aStreamDic)
    {
    CRichText* text=CRichText::NewL(iParaFormatLayer,iCharFormatLayer);
    CleanupStack::PushL(text);
    TStreamId id=aStreamDic.At(KUidChattst);
    text->RestoreL(aStore,id);
    iEditStore=CONST_CAST(CStreamStore*,&aStore);
    CleanupStack::Pop();
    delete iRichTextOutput;
    iRichTextOutput=text;
    }

    void CChattstDocument::StoreL(CStreamStore& aStore,CStreamDictionary& aStreamDic) const
    {
    TStreamId id=iRichTextOutput->StoreL(aStore);
    aStreamDic.AssignL(KUidChattst,id);
    }

    TBool CChattstDocument::FileNameExists(const TFileName& aFileName)const
    {
    return (aFileName==Process()->MainDocFileName());
    }

    CEikAppUi* CChattstDocument::CreateAppUiL()
    {
    return(new(ELeave) CChattstAppUi);
    }


    // Checks whether the specified file name is the same as the
    // file whose content is currently being viewed (i.e. open)
    //
    TBool CChattstDocument::IsCurrentFileName(const TFileName& aFileName)const
    {
    return (aFileName==Process()->MainDocFileName());
    }


    ////////////////////////////////////////////////////////////////
    //
    // App UI class, CChattstAppUi
    //
    ////////////////////////////////////////////////////////////////

    void CChattstAppUi::ConstructL()
    {
    BaseConstructL();
    // iAppView->ResetExampleL(iDoc);
    iDoc = (CChattstDocument*)iDocument;
    iAppView = new(ELeave) CChattstAppView;
    iAppView->ConstructL(iDoc);
    iAppView->SetRect(ClientRect());
    iAppView->ActivateL();
    }



    CChattstAppUi::~CChattstAppUi()
    {
    delete iAppView;
    }

    // Called by the UI framework when a command is issued.
    // In this example, a command can originate through a
    // hot-key press or by selection of a menu item or by
    // pressing a CBA button.
    // The command Ids are defined in the .hrh file.
    // EEikCmdExit and the file ccommand ids are defined
    // by the UI framework from eikon.hrh
    //
    void CChattstAppUi::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
    {
    // File commands. The command ids are standard
    // definitions supplied by the UI.
    case EEikCmdFileOpen:
    CmdFileOpenL();
    break;

    case EEikCmdFileNew:
    CmdFileNewL();
    break;

    case EEikCmdFileSave:
    CmdFileSaveL();
    break;

    case EEikCmdFileSaveAs:
    CmdFileSaveAsL();
    break;

    case EEikCmdFileRevert:
    CmdFileRevertL();
    break;
    // Exit the application. This is
    // implemented by the UI framework.
    // Don't forget to save any changes before going !
    case EEikCmdExit:
    SaveL();
    Exit();
    break;
    }
    }

    // Respond to events reported by our control; this implies a
    // change to the model
    //
    void CChattstAppUi:: HandleControlEventL(CCoeControl* /*aControl*/,TCoeEvent aEventType)
    {
    if (aEventType==EEventStateChanged)
    {
    SetDocChanged();
    }
    }


    // Handle user request to revert to previously saved file
    //
    //
    void CChattstAppUi::CmdFileRevertL()
    {
    // Check whether any changes have been made to
    // current data; if so, notify the user about
    // the potential loss of changes.
    if (Document()->HasChanged())
    {
    HBufC* title = iEikonEnv->AllocReadResourceLC(R_EXAMPLE_TITLE_REVERT);
    HBufC* message = iEikonEnv->AllocReadResourceLC(R_EXAMPLE_MESSAGE_LOSTCHANGES);
    TInt retValue = CCknConfirmationDialog::RunDlgLD(*title,*message);
    CleanupStack::PopAndDestroy(2);
    // If user confirms revert operation, revert
    // to previous file and update the UI
    // to reflect the changes.
    if(retValue)
    {
    // Revert to previous file and update the
    // UI to reflect the changes.
    ((CChattstDocument*)Document())->DoFileRevertL();
    HandleModelChangeL();
    }
    }
    else
    {
    iEikonEnv->InfoMsg(R_EXAMPLE_TEXT_DOCUMENT_UNCHANGED);
    }
    }

    // Handle user request to save model data to a named file.
    //
    void CChattstAppUi::CmdFileSaveAsL()
    {
    // Set an initial value for the folder to
    // contain the new file and pass this to
    // the dialog box to display in the "Folder" field.
    // TFileName fileName=iEikonEnv->Process()->MainDocFolder();

    fileName=iEikonEnv->Process()->MainDocFolder();
    SetInitialPathL(fileName);
    // Launch the dialog so that the user can enter
    // the 'save as' file name.
    if (CCknSaveAsFileDialog::RunDlgLD(fileName))
    {
    // If the dialog returns a filename that is the same as
    // the current file, we're really doing a "Save",
    // not a "Save as".
    if (((CChattstDocument*)Document())->IsCurrentFileName(fileName))
    {
    CmdFileSaveL();
    return;
    }

    // Delete the file, if it already exists. this makes sure
    // we dont get a NOTE ->FILE ALREADY EXISTS ERROR.
    TEntry entry;
    if (!iEikonEnv->FsSession().Entry(fileName,entry))
    User::LeaveIfError(iEikonEnv->FsSession().Delete(fileName));

    // Save to a new file. This behaviour is implemented
    // by the document object
    ((CChattstDocument*)Document())->DoFileSaveToNewL(fileName);
    iEikonEnv->InfoMsg(R_EIK_TBUF_FILE_SAVED);

    // Update the UI to reflect the changes
    HandleModelChangeL();
    }
    }

    // file handling

    TBool CChattstAppUi::ProcessCommandParametersL(TApaCommand aCommand,TFileName& aDocumentName,const TDesC8& /*aTail*/)
    {
    return CEikAppUi::ProcessCommandParametersL(aCommand,aDocumentName);
    }

    // Handle user request to create a new file.
    //
    void CChattstAppUi::CmdFileNewL()
    {
    // Set an initial value for the folder to
    // contain the new file and pass this to
    // the dialog box to display in the "Folder" field.
    TFileName fileName;
    SetInitialPathL(fileName);

    // Launch the dialog so that the user can enter
    // a new file name.
    if (CCknNewFileDialog::RunDlgLD(fileName))
    {
    CreateFileL(fileName);
    }
    }

    // Handle user request to open a file.
    //
    void CChattstAppUi::CmdFileOpenL()
    {
    // Set an initial value for the folder to
    // contain the new file and pass this to
    // the dialog box to display in the "Folder" field.
    TFileName filename;
    SetInitialPathL(filename);

    // Launch the dialog so that the user can enter
    // a file name. All drives are shown. Restricted to
    // files associated with the Application UID.
    if (CCknOpenFileDialog::RunDlgLD(filename))
    {
    OpenFileL(filename);
    }
    }


    // Handle user request to save model data to the current file.
    //
    void CChattstAppUi::CmdFileSaveL()
    {
    if (iContainerAppUi)
    return;

    // if (iDocument->HasChanged())
    if (((CEikProcess*)(Document()->Process()))->MainStore())
    {
    SaveL();
    iEikonEnv->InfoMsg(R_EIK_TBUF_FILE_SAVED);
    }
    else
    {
    iEikonEnv->InfoMsg(R_EXAMPLE_TEXT_DOCUMENT_UNCHANGED);
    }
    }


    // Open the file with the specified path/name.
    //
    void CChattstAppUi::OpenFileL(const TFileName& aFileName)
    {
    // Save any changes to the current file before
    // opening the other file. Note that SaveAnyChangesL()
    // is implemented by CEikAppui
    SaveAnyChangesL();
    // Call the document function to open a file
    STATIC_CAST(CChattstDocument*,iDocument)->DoOpenFileL(aFileName);
    // Update the UI to reflect the changes
    HandleModelChangeL();
    }


    // Create a new file with the specified path/name.
    void CChattstAppUi::CreateFileL(TFileName& aFileName)
    {
    // Save any changes to the current file before
    // creating the new one. Note that SaveAnyChangesL()
    // is implemented by CEikAppui
    SaveAnyChangesL();
    // Call the document function to create a new file
    CChattstDocument* documentPtr=static_cast <CChattstDocument*> (iDocument);
    documentPtr->DoNewFileL(aFileName);
    // Update the UI to reflect the changes
    HandleModelChangeL();
    }


    //
    // Create a new file with the specified path/name.
    //
    void CChattstAppUi::HandleModelChangeL()
    {
    iRichTextOutput=((CChattstDocument*)iDocument)->Text();
    // Redraw the application view based on the new model.
    iAppView->DrawNow();

    // Update the file name in the task list to show the current file.
    iEikonEnv->UpdateTaskNameL();
    }


    // Returns the current file's path name
    //
    TFileName CChattstAppUi::CurrentFilePath() const
    {
    TParsePtrC parser(Document()->Process()->MainDocFileName());
    return parser.DriveAndPath();
    }


    // Returns the current file's path name
    //
    void CChattstAppUi::SetInitialPathL(TFileName& aFileName) const
    // checks that the initial path exists, creating it if not
    // if the path was read-only a default is used instead
    {
    aFileName=CurrentFilePath();
    TRAPD(ret,ConeUtils::EnsurePathExistsL(aFileName) );
    if (ret!=KErrNone)
    {// if the path is eg read-only default to C:
    aFileName=KBossDefaultFilePath;
    ConeUtils::EnsurePathExistsL(aFileName);
    }
    }


    //
    //
    // Function: CChatAppUi::HandleKeyEventL
    //
    // Send key events to display and remote chat.
    //
    // @param const TKeyEvent& aKeyEvent
    // The key code.
    // @param TEventCode aType
    // The event type. We only send EEventKey events to remote
    // chat. This is because we get more than 1 key event per
    // key press (up, down, event)
    //
    // @returns None
    //
    TKeyResponse CChattstAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    iAppView->KeyEventL(aKeyEvent, aType); // send to view for display
    if (aType == EEventKey)
    {
    TBuf<1> dispChar;
    TBuf8<4> utf8Char;
    dispChar.Append(aKeyEvent.iCode);
    }
    return EKeyWasConsumed;
    }


    //
    // Function: CChatAppUi::MessageGenerated
    //
    // Part of the MChatNotify interface. Used by the chat engine to display
    // messages.
    //
    // @param const TDesC& aMessage
    // The error message.
    //
    // @returns None
    //
    void CChattstAppUi::MessageGenerated(const TDesC& aMessage)
    {
    iAppView->Print(aMessage); // display message in window
    }





    ////////////////////////////////////////////////////////////////
    //
    // Application view class, CChattstAppView
    //
    ////////////////////////////////////////////////////////////////

    void CChattstAppView::ConstructL(CChattstDocument* aDoc)
    {
    iDoc = aDoc;
    CreateWindowL();
    Window().SetShadowDisabled(EFalse);
    SetBlank();
    // construct input window
    const CFont* font = CEikonEnv::Static()->NormalFont();
    TFontSpec fontspec = font->FontSpecInTwips();

    TCharFormat defaultCharFormat( fontspec.iTypeface.iName, fontspec.iHeight );
    TCharFormatMask defaultCharFormatMask;

    iParaFormat = CParaFormatLayer::NewL();
    iCharFormat = CCharFormatLayer::NewL(defaultCharFormat,defaultCharFormatMask);

    defaultCharFormatMask.SetAttrib(EAttFontTypeface);
    defaultCharFormatMask.SetAttrib(EAttFontHeight);
    delete iInputWindow;

    //Create bordered edit windows.
    iInputWindow = new (ELeave) CEikRichTextEditor(TGulBorder(TGulBorder::EShallowRaised));
    iRichTextInput=CRichText::NewL(iParaFormat, iCharFormat);
    }


    CChattstAppView::~CChattstAppView()
    {
    delete iDoc;
    delete iInputWindow;
    delete iRichTextInput;
    }

    void CChattstAppView::SizeChanged()
    {
    TRect left = Rect();
    iInputWindow->ConstructL(this,0,0,EEikEdwinOwnsWindow|EEikEdwinInclusiveSizeFixed|
    EEikEdwinKeepDocument|EEikEdwinUserSuppliedText|
    EEikEdwinLineCursor|EEikEdwinAlwaysShowSelection);
    iInputWindow->SetDocumentContentL(*iRichTextInput,CEikEdwin::EUseText);
    iInputWindow->SetRect(left);
    iInputWindow->ActivateL();
    iInputWindow->SetFocus(ETrue);
    }

    //
    //
    // Function: CChattstView::KeyEventL
    //
    // Overridden to intercept keypress's and pass on to remote device.
    // @param const TKeyEvent& aKeyEvent
    // The keypress
    // @param TEventCode aType
    // The event code. (Key up/down etc)
    //
    // @returns None
    //
    void CChattstAppView::KeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    iInputWindow->SetFocus(ETrue);
    iInputWindow->OfferKeyEventL(aKeyEvent, aType);
    }


    //
    // Function: CChattstView::Print
    //
    // Simple output method to simulate a console printf.
    //
    // @param const TDesC& aMessage
    // the message to output.
    // @returns None
    //
    void CChattstAppView::Print(const TDesC& aMessage)
    {
    // Add message 1 char at a time.
    TKeyEvent aKeyEvent;
    for (int i = 0; i < aMessage.Length(); i++)
    {
    aKeyEvent.iCode = aMessage[i];
    aKeyEvent.iScanCode = aMessage[i];
    }
    }


    void CChattstAppView:raw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    // Clear the application view
    gc.Clear();
    // Draw a rectangle round the edge of the application view.
    TRect drawRect=Rect();
    drawRect.Shrink(10,10);
    gc.DrawRect(drawRect);
    }
    ========================================================

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    31
    KERN EXEC 3

    I think KERN EXEC occurs because you are trying to delete

    CChattstAppView::~CChattstAppView()
    {
    delete iDoc;
    delete iInputWindow;
    delete iRichTextInput;
    }

    iDoc here.
    iDoc is a pointer to Document of AppUi, and AppUi is also trying to delete document.

    Comment out "delete iDoc" and try again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •