Re: FileChooser overwrite_confirmation



On Feb 11, 2008, at 10:09 AM, Jeffrey Ratcliffe wrote:

My application forces the extension of saved files, in this case, to
be .pdf. Up to now, I have used my own dialog box to check if a file
should be overwritten. Just, spotting that the FileChooser has
set_do_overwrite_confirmation (TRUE) - and wanting to use it for
consistency with other Gtk+ apps, I am having trouble with the
possibility that the user types in a new filename that, without an
extension, does not already exist, but with one, does.

I can't seem to get FileChooser to recheck the filename. Should I be
emitting a specific signal?
The docs for gtk_file_chooser_set_filename() don't mention it, but it  
doesn't perform its job immediately.  So, the basic problem is that  
you're not waiting for the filename change to take effect.
Now, in testing this, i noticed that if i used a nonexistent name with  
no extension, i got into an infinite loop of the dialog pressing its  
own button.  (Oooh, kinky.  :-)  The answer to that lies in the gtk+  
docs for this function (example translated to perl):
Note that the file must exist, or nothing will be done except for the directory change.
If you are implementing a "File/Save As..." dialog, you should use  
this function if you already have a file name to which the user may  
save; for example, when the user opens an existing file and then  
does "File/Save As..." on it.  If you don't have a file name already  
-- for example, if the user just created a new file and is saving it  
for the first time, do not call this function.  Instead, use  
something similar to this:
  if (document_is_new) {
      # the user just created a new document
      $chooser->set_current_folder ($default_folder_for_saving);
      $chooser->set_current_name ("Untitled document");
  } else {
      # the user edited an existing document
      $chooser->set_filename ($existing_filename);
  }

So, i think this means for your particular situation, in which you're munging the user's choice, you need to do a file existence check for yourself.
I got it to work reasonably well by changing your response handler to  
this:
$file_chooser -> signal_connect (response => sub {
    my ($dialog, $response) = @_;
    if ($response eq 'ok') {
        my $filename = $file_chooser -> get_filename;
        if ($filename !~ /\.pdf$/i) {
            $filename = $filename.'.pdf';
            if (-f $filename) {
# File exists; get the file chooser to ask the user to confirm.
                $file_chooser -> set_filename($filename);
                # Give the name change time to take effect.
Glib::Idle->add (sub { $file_chooser->response ('ok'); });
                return;
            }
        }
        $file_chooser -> destroy;
        #[do something with file]
        print "filename $filename\n";
    }
});

--
It's all very complicated and would take a scientist to explain it.
  -- MST3K





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]