03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Update view<br />

iAppView->DrawNow();<br />

}<br />

The normal sequence of processing here is that I invoke the dialog (using the terse syntax<br />

that was covered in <strong>Chapter</strong> 10), write the file, and redraw the app view to reflect the new<br />

iText value.<br />

If WriteFileL() leaves, I handle it by deleting iText and setting its pointer to 0. I also<br />

delete the file. Then I redraw the app view and use User::Leave() to propagate the error<br />

so that Uikon can display an error message corresponding to the error code. Test this, if you<br />

like, by inserting User::Leave(KErrDiskFull) somewhere in WriteFileL().<br />

The trap handler has one virtue: it lets the user know what's going on. It's still not good<br />

enough for serious application use, though, because this approach loses user and file data.<br />

It's a cardinal rule that Symbian OS shouldn't do that. A better approach would be to do the<br />

following:<br />

� Write the new data to a temporary file: if this fails, keep the user data in RAM, but<br />

delete the temporary file.<br />

� Delete the existing file: this is very unlikely to fail, but if it does, keep the user data in<br />

RAM but delete the temporary file.<br />

� Rename the temporary file: this is very unlikely to fail, and if it does, we're in trouble.<br />

Keep the user data in RAM anyway.<br />

I would need to restructure my program to achieve this; the application architecture's<br />

framework for load/save and embeddable documents looks after these issues for you.<br />

For database-type documents, you face these issues with each entry in the database. They<br />

are managed by the permanent file store class, which we'll encounter below.<br />

13.4.3 Reading it Back<br />

The code to read the data we have written is similar. Firstly, we read the filename from a<br />

dialog and then use OkToExitL() to do some checking. This time, the code is much easier<br />

and I'll present it in one segment:<br />

TBool CExampleReadFileDialog::OkToExitL(TInt /* aKeycode */)<br />

// Termination<br />

{<br />

// Get filename<br />

CEikFileNameEditor* fnamed=static_cast<br />

(Control(EExampleControlIdFileName));<br />

HBufC* fileName = fnamed->GetTextInHBufL();<br />

// Check it's even been specified<br />

if(!fileName)<br />

{<br />

TryChangeFocusToL(EExampleControlIdFileName);<br />

iEikonEnv-<br />

>LeaveWithInfoMsg(R_EIK_TBUF_NO_FILENAME_SPECIFIED);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!