Move emergencyWrite() from BufferList to Buffer.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@31056 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2009-08-15 16:34:54 +00:00
parent 45600e36eb
commit c5487a3682
5 changed files with 66 additions and 72 deletions

View File

@ -327,7 +327,7 @@ Buffer::~Buffer()
if (theBufferList().isLoaded(child))
theBufferList().releaseChild(this, child);
}
// clear references to children in macro tables
d->children_positions.clear();
d->position_to_children.clear();
@ -942,6 +942,63 @@ bool Buffer::writeFile(FileName const & fname) const
}
docstring Buffer::emergencyWrite()
{
// No need to save if the buffer has not changed.
if (isClean())
return docstring();
string const doc = isUnnamed() ? onlyFilename(absFileName()) : absFileName();
docstring user_message = bformat(
_("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
// We try to save three places:
// 1) Same place as document. Unless it is an unnamed doc.
if (!isUnnamed()) {
string s = absFileName();
s += ".emergency";
LYXERR0(" " << s);
if (writeFile(FileName(s))) {
markClean();
user_message += bformat(_(" Saved to %1$. Phew.\n"), from_utf8(s));
return user_message;
} else {
user_message += _(" Save failed! Trying again...\n");
}
}
// 2) In HOME directory.
string s = addName(package().home_dir().absFilename(), absFileName());
s += ".emergency";
lyxerr << ' ' << s << endl;
if (writeFile(FileName(s))) {
markClean();
user_message += bformat(_(" Saved to %1$. Phew.\n"), from_utf8(s));
return user_message;
}
user_message += _(" Save failed! Trying yet again...\n");
// 3) In "/tmp" directory.
// MakeAbsPath to prepend the current
// drive letter on OS/2
s = addName(package().temp_dir().absFilename(), absFileName());
s += ".emergency";
lyxerr << ' ' << s << endl;
if (writeFile(FileName(s))) {
markClean();
user_message += bformat(_(" Saved to %1$. Phew.\n"), from_utf8(s));
return user_message;
}
user_message += _(" Save failed! Bummer. Document is lost.");
// Don't try again.
markClean();
return user_message;
}
bool Buffer::write(ostream & ofs) const
{
#ifdef HAVE_LOCALE

View File

@ -171,6 +171,9 @@ public:
/// Write document to stream. Returns \c false if unsuccesful.
bool write(std::ostream &) const;
/// save emergency file
/// \return a status message towards the user.
docstring emergencyWrite();
/// Write file. Returns \c false if unsuccesful.
bool writeFile(support::FileName const &) const;

View File

@ -219,70 +219,10 @@ void BufferList::updateIncludedTeXfiles(string const & masterTmpDir,
void BufferList::emergencyWriteAll()
{
for_each(bstore.begin(), bstore.end(),
bind(&BufferList::emergencyWrite, this, _1));
}
docstring BufferList::emergencyWrite(Buffer * buf)
{
// Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert
// compare with 0 to avoid pointer/interger comparison
// ::assert(buf != 0);
if (!buf)
return _("No file open!");
// No need to save if the buffer has not changed.
if (buf->isClean())
return docstring();
string const doc = buf->isUnnamed()
? onlyFilename(buf->absFileName()) : buf->absFileName();
docstring user_message = bformat(
_("LyX: Attempting to save document %1$s\n"), from_utf8(doc));
// We try to save three places:
// 1) Same place as document. Unless it is an unnamed doc.
if (!buf->isUnnamed()) {
string s = buf->absFileName();
s += ".emergency";
lyxerr << " " << s << endl;
if (buf->writeFile(FileName(s))) {
buf->markClean();
user_message += _(" Save seems successful. Phew.\n");
return user_message;
} else {
user_message += _(" Save failed! Trying...\n");
}
}
// 2) In HOME directory.
string s = addName(package().home_dir().absFilename(), buf->absFileName());
s += ".emergency";
lyxerr << ' ' << s << endl;
if (buf->writeFile(FileName(s))) {
buf->markClean();
user_message += _(" Save seems successful. Phew.\n");
return user_message;
}
user_message += _(" Save failed! Trying...\n");
// 3) In "/tmp" directory.
// MakeAbsPath to prepend the current
// drive letter on OS/2
s = addName(package().temp_dir().absFilename(), buf->absFileName());
s += ".emergency";
lyxerr << ' ' << s << endl;
if (buf->writeFile(FileName(s))) {
buf->markClean();
user_message += _(" Save seems successful. Phew.\n");
return user_message;
}
user_message += _(" Save failed! Bummer. Document is lost.");
return user_message;
BufferStorage::const_iterator it = bstore.begin();
BufferStorage::const_iterator const en = bstore.end();
for (; it != en; ++it)
(*it)->emergencyWrite();
}

View File

@ -69,12 +69,6 @@ public:
/// emergency save for all buffers
void emergencyWriteAll();
/// save emergency file for the given buffer
/**
* \return a status message towards the user.
*/
docstring emergencyWrite(Buffer * buf);
/// return true if no buffers loaded
bool empty() const;

View File

@ -1319,7 +1319,7 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
case BufferException: {
Buffer * buf = current_view_->buffer();
docstring details = e.details_ + '\n';
details += theBufferList().emergencyWrite(buf);
details += buf->emergencyWrite();
theBufferList().release(buf);
details += "\n" + _("The current document was closed.");
Alert::error(e.title_, details);