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.
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)
{
void CChattstDocument::ConstructL()
{
TCharFormatMask defaultCharFormatMask;
TCharFormat defaultCharFormat(_L("Arial",178); // !! should seed by some other means
defaultCharFormatMask.SetAttrib(EAttFontTypeface);
defaultCharFormatMask.SetAttrib(EAttFontHeight);
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);
}
// 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());
}
// 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();
}
}
// 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
}