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.

The code makes a simple MkDirAll() call. This time you can see why RFs functions don't<br />

leave with an error code: I'm happy if either the directories didn't exist and they were created<br />

successfully, or they did exist and nothing happened. Otherwise, I initiate a leave.<br />

Now things get more delicate. I tentatively create the file, by opening it:<br />

// Check whether it's going to be possible to create the<br />

file for writing<br />

RFile file;<br />

err = file.Create(iCoeEnv->FsSession(), *fileName, EFileWrite);<br />

if(err != KErrNone && err != KErrAlreadyExists)<br />

User::Leave(err); // No need to close file, since it didn't open<br />

RFile::Create() creates a new file if possible. If the file was already there, err contains<br />

KErrAlreadyExists: I want to know whether the user really wants to replace this file, and<br />

I'll check that in the next step. If the file wasn't there, but was created successfully by<br />

RFile::Create(), then err is set to KErrNone. In any other case, I leave.<br />

If I had wanted to replace the file without checking with the user, I would have used<br />

RFile::Replace() to open the file. As it is, I want to check with the user:<br />

// Check whether the user wants to replace the file,<br />

if it already exists<br />

if(err == KErrAlreadyExists)<br />

{<br />

if(iEikonEnv->QueryWinL(R_EIK_TBUF_FILE_REPLACE_CONFIRM))<br />

else<br />

}<br />

User::LeaveIfError(file.Replace(iCoeEnv->FsSession(),<br />

*fileName,<br />

EFileWrite));<br />

iEikonEnv->LeaveWithInfoMsg(0); // Let user try again<br />

If the attempt to create the file revealed that it already existed, I use a UIQ query dialog to<br />

confirm with the user. If the user insists, then I open the file again with RFile::Replace().<br />

Any errors this time must be genuine errors, so I don't have to check specific codes – I<br />

simply enclose the call with User::LeaveIfError().<br />

But if the user didn't want to replace the file, I leave silently with iEikonEnv-<br />

>LeaveWithInfoMsg(0). The leave helps to ensure that anything I've pushed to the<br />

cleanup stack is popped and destroyed. The 0 indicates that I'm not passing a resource ID of<br />

a message, because I don't want a message – none is needed, since the user knows exactly<br />

what has happened: they just replied No to a query.<br />

Now I've done all the checks I need to make. I've verified user input and I've verified that I<br />

can create the file for writing.<br />

I prefer to separate UI code (like this) from engine code (which actually does things, like<br />

writing files), so I don't write the file from this function. Instead, I pass the parameters back to<br />

the app UI, which will process them when I return:

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

Saved successfully!

Ooh no, something went wrong!