Confirming file overwrite with Gtk::FileChooser



I remember last week someone working on this, here's what I did:

bool IO::uri_exists (const Glib::ustring &uri)
{
    try {
	Glib::RefPtr<Gnome::Vfs::FileInfo> fileinfo = Gnome::Vfs::Handle::get_file_info (uri);
	return true;
    } catch (...) {
    }

    return false;
}

bool UI::confirm_save (Gtk::FileChooserDialog &file_chooser)
{
    if (file_chooser.run () == Gtk::RESPONSE_ACCEPT)
    {
	Glib::ustring uri = file_chooser.get_uri ();
	if (IO::uri_exists (uri))
	{
	    Glib::ScopedPtr<char> message (
		g_strdup_printf (_("<b><big>\"%s\" already exists</big></b>\n"
				   "Would you like to overwrite it?"),
				 UI::visible_filename (uri).c_str ()));
	    Gtk::MessageDialog *confirm = new Gtk::MessageDialog (file_chooser,
								  message.get (), true,
								  Gtk::MESSAGE_QUESTION,
								  Gtk::BUTTONS_YES_NO,
								  true);
	    int response = confirm->run ();
	    delete confirm;

	    if (response == Gtk::RESPONSE_YES)
		return true;
	    else
		return confirm_save (file_chooser);

	} else {
	    return true;
	}
    }

    return false;
}

--
   .--= ULLA! =---------------------.   `We are not here to give users what
   \     http://cactus.rulez.org     \   they want'  -- RMS, at GUADEC 2001
    `---= cactus cactus rulez org =---'
A true free thinking rebel has no problem doing exactly what the huge masses of idiots do, if he happens to enjoy it.



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