fix for open of non-existent file, writeFile() change

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4832 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
John Levon 2002-08-01 22:26:30 +00:00
parent d91ffd8d78
commit b14e9baa57
10 changed files with 45 additions and 43 deletions

View File

@ -1,3 +1,14 @@
2002-08-01 John Levon <levon@movementarian.org>
* buffer.h:
* buffer.C (writeFile): don't output alerts, caller
handles this
* bufferlist.C:
* lyx_cb.C: from above
* lyxfunc.C: allow to open non-existent files
2002-07-31 John Levon <levon@movementarian.org>
* lyxserver.C: don't let incidental errors get

View File

@ -1812,7 +1812,7 @@ bool Buffer::save() const
}
}
if (writeFile(fileName(), false)) {
if (writeFile(fileName())) {
markLyxClean();
removeAutosaveFile(fileName());
} else {
@ -1826,46 +1826,19 @@ bool Buffer::save() const
}
// Returns false if unsuccesful
bool Buffer::writeFile(string const & fname, bool flag) const
bool Buffer::writeFile(string const & fname) const
{
// if flag is false writeFile will not create any GUI
// warnings, only cerr.
// Needed for autosave in background or panic save (Matthias 120496)
if (read_only && (fname == fileName())) {
// Here we should come with a question if we should
// perform the write anyway.
if (flag)
lyxerr << _("Error! Document is read-only: ")
<< fname << endl;
else
Alert::alert(_("Error! Document is read-only: "),
fname);
return false;
}
FileInfo finfo(fname);
if (finfo.exist() && !finfo.writable()) {
// Here we should come with a question if we should
// try to do the save anyway. (i.e. do a chmod first)
if (flag)
lyxerr << _("Error! Cannot write file: ")
<< fname << endl;
else
Alert::err_alert(_("Error! Cannot write file: "),
fname);
return false;
}
ofstream ofs(fname.c_str());
if (!ofs) {
if (flag)
lyxerr << _("Error! Cannot open file: ")
<< fname << endl;
else
Alert::err_alert(_("Error! Cannot open file: "),
fname);
return false;
}

View File

@ -148,7 +148,7 @@ public:
bool save() const;
/// Write file. Returns \c false if unsuccesful.
bool writeFile(string const &, bool) const;
bool writeFile(string const &) const;
///
void writeFileAscii(string const & , int);

View File

@ -315,7 +315,7 @@ void BufferList::emergencyWrite(Buffer * buf)
string s = buf->fileName();
s += ".emergency";
lyxerr << " " << s << endl;
if (buf->writeFile(s, true)) {
if (buf->writeFile(s)) {
buf->markLyxClean();
lyxerr << _(" Save seems successful. Phew.") << endl;
return;
@ -328,7 +328,7 @@ void BufferList::emergencyWrite(Buffer * buf)
string s = AddName(GetEnvPath("HOME"), buf->fileName());
s += ".emergency";
lyxerr << " " << s << endl;
if (buf->writeFile(s, true)) {
if (buf->writeFile(s)) {
buf->markLyxClean();
lyxerr << _(" Save seems successful. Phew.") << endl;
return;
@ -342,7 +342,7 @@ void BufferList::emergencyWrite(Buffer * buf)
s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
s += ".emergency";
lyxerr << " " << s << endl;
if (buf->writeFile(s, true)) {
if (buf->writeFile(s)) {
buf->markLyxClean();
lyxerr << _(" Save seems successful. Phew.") << endl;
return;

View File

@ -1,3 +1,7 @@
2002-08-01 John Levon <levon@movementarian.org>
* ControlSendto.C: writeFile() change
2002-08-01 John Levon <levon@movementarian.org>
* ControlSpellchecker.h:

View File

@ -122,7 +122,7 @@ void ControlSendto::apply()
if (!lv_.buffer()->tmppath.empty())
filename = AddName(lv_.buffer()->tmppath, filename);
lv_.buffer()->writeFile(filename, true);
lv_.buffer()->writeFile(filename);
} else {
Exporter::Export(lv_.buffer(), format_->name(), true, filename);

View File

@ -1,3 +1,7 @@
2002-08-01 John Levon <levon@movementarian.org>
* FormDocument.C: writeFile() change
2002-08-01 John Levon <levon@movementarian.org>
* FormPreferences.h:

View File

@ -407,7 +407,7 @@ bool saveParamsAsDefault(BufferParams const &params)
// add an empty paragraph. Is this enough?
defaults.paragraph = new Paragraph;
return defaults.writeFile(defaults.fileName(), false);
return defaults.writeFile(defaults.fileName());
}
} //namespace

View File

@ -278,7 +278,7 @@ void AutoSave(BufferView * bv)
string const tmp_ret = lyx::tempName(string(), "lyxauto");
if (!tmp_ret.empty()) {
bv->buffer()->writeFile(tmp_ret, 1);
bv->buffer()->writeFile(tmp_ret);
// assume successful write of tmp_ret
if (!lyx::rename(tmp_ret, fname)) {
failed = true;
@ -293,7 +293,7 @@ void AutoSave(BufferView * bv)
if (failed) {
// failed to write/rename tmp_ret so try writing direct
if (!bv->buffer()->writeFile(fname, 1)) {
if (!bv->buffer()->writeFile(fname)) {
// It is dangerous to do this in the child,
// but safe in the parent, so...
if (pid == -1)

View File

@ -1687,16 +1687,26 @@ void LyXFunc::open(string const & fname)
// get absolute path of file and add ".lyx" to the filename if
// necessary
string const fullpath = FileSearch(string(), filename, "lyx");
if (fullpath.empty()) {
Alert::alert(_("Error"), _("Could not find file"), filename);
return;
if (!fullpath.empty()) {
filename = fullpath;
}
filename = fullpath;
// loads document
string const disp_fn(MakeDisplayPath(filename));
// if the file doesn't exist, let the user create one
FileInfo const f(filename, true);
if (!f.exist()) {
if (!Alert::askQuestion(_("No such file"), disp_fn,
_("Start a new document with this filename ?"))) {
owner->message(_("Canceled"));
return;
}
// the user specifically chose this name. Believe them.
Buffer * buffer = bufferlist.newFile(filename, "", true);
owner->view()->buffer(buffer);
return;
}
ostringstream str;
str << _("Opening document") << ' ' << disp_fn << "...";