File save question


#1

I’m using FileChooser::.browseForFileToSave(true) in the process of saving off a PropertiesFile. In creating the FileChooser I specify a path and a file name, something like foo.set

If foo.set is already in the directory the dialog shows upon opening, and the user types “foo” in as the file name to save, I don’t see the warning sheet appear asking if the user wants to overwrite the existing file.

I do see the warning if I type in “foo.set” in the dialog.

This seems a little deceiving. If I type “foo”, the new file will replace “foo.set”, but I won’t get warned. If I type “foo.set”, I will get warned, but the file doesn’t get saved to “foo.set.set”.

Does all this make sense? Is there a way to add the file extension to the check being made for overwriting an existing file?


#2

I assume you’re adding the file extension yourself, after the dialog box has returned “foo”? If so, then it’s up to you to check for overwriting - you can’t expect the dialog to know what you’re going to do with the filename it gives you!


#3

The file extension is being added automatically. I’m creating the FileChooser thus:

FileChooser theFileChooser ("Select a location to save",
                               File (File::userDocumentsDirectory + T("/my/folder/")+ theFileName),
                               "*.bar");

What the user sees is the name, sans extension, but the file that’s saved is the name the user types plus “.bar”. Since the FileChooser knows the extension, and thus where the file will be saved, I assumed it could check for overwriting before returning.

An equally acceptable solution would be to show the file extension in the dialog, with the name highlighted. In that way the user could see the full name the file will be saved under, could hit delete to rename the file, leaving only the extension, and when there’s a conflict, the existing code will catch it.

Hmm. Now that I’m reading the header, I see I’m passing in my extension into the filePatternsAllowed field. I’m not sure if that’s the correct thing to do for a browseForFileToSave() call. It does work, however…


#4

Well you can certainly pass some file patterns to a save dialog, which it’ll use to filter the files it displays, but I wouldn’t expect it to use them to actually change the filename returned. Are you sure it’s actually returning “foo.bar” if you only enter “foo”?


#5

OK. I’m an idiot. I was adding the extension afterwards, when I created the File to use in the properties file.

So, I tried to correct that. I’m now saving to the location returned by FileChooser.getResult(), and I’m creating the FileChooser using:

FileChooser theFileChooser ("Select a location to save",
						File (File::userDocumentsDirectory + T("/my/folder/Untitled.bar")));

I’d expect the save dialog to appear with the name “Untitled.bar” in the name field, but it comes up saying “Untitled” instead. If I clear it and type “foo”, the return path ends with “foo”, not “foo.bar”

What I’m really trying to do is to force the user to use the file extension of my choosing (at least by default), and to use the name + extension to check for conflicts when saving a file. Right now the extension is hidden, in the save dialog, which is fine, but if the user changes the default name, it seems the extension is lost, which isn’t good. If I add the extension afterwards, then the overwrite warning can’t happen in the save dialog. Is there a good way to accomplish what I’m after?


#6

Told you so.

I don’t think there’s a way to get all the platform-specific stuff to force a suffix. I’d say just add it yourself, but warn them about overwriting.