more Alert:: work

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6625 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
John Levon 2003-03-29 09:02:08 +00:00
parent e8c9544a2d
commit 20215c83e7
16 changed files with 262 additions and 93 deletions

View File

@ -43,6 +43,7 @@
#include "support/lyxfunctional.h" // equal_1st_in_pair
#include "support/types.h"
#include "support/lyxalgo.h" // lyx_count
#include "BoostFormat.h"
#include <fstream>
@ -312,8 +313,19 @@ bool BufferView::insertLyXFile(string const & filen)
ifstream ifs(fname.c_str());
if (!ifs) {
Alert::err_alert(_("Error! Cannot open specified file:"),
MakeDisplayPath(fname, 50));
string const error = strerror(errno);
string const file = MakeDisplayPath(fname, 50);
#if USE_BOOST_FORMAT
boost::format fmt(_("Could not open the specified document\n%1$s\ndue to the error: %2$s"));
fmt % file;
fmt % error;
string text = fmt.str();
#else
string text = _("Could not open the specified document\n");
text += file + _(" due to the error: ");
text += error;
#endif
Alert::error(_("Could not open file"), text);
return false;
}

View File

@ -1,3 +1,10 @@
2003-03-29 John Levon <levon@movementarian.org>
* lyx_cb.C:
* BufferView.C:
* buffer.C: warnings pushed down from support/,
kill err_alert
2003-03-29 John Levon <levon@movementarian.org>
* lyxfunc.C: safety check for C-r (revert)

View File

@ -149,8 +149,15 @@ Buffer::~Buffer()
if (users)
users->buffer(0);
if (!tmppath.empty()) {
DestroyBufferTmpDir(tmppath);
if (!tmppath.empty() && destroyDir(tmppath) != 0) {
#if USE_BOOST_FORMAT
boost::format fmt = _("Could not remove the temporary directory %1$s");
fmt % tmppath;
string msg = fmt.str();
#else
string msg = _("Could not remove the temporary directory ") + tmppath;
#endif
Alert::warning(_("Could not remove temporary directory"), msg);
}
paragraphs.clear();
@ -954,7 +961,19 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
{
ofstream ofs(fname.c_str());
if (!ofs) {
Alert::err_alert(_("Error: Cannot write file:"), fname);
string const error = strerror(errno);
string const file = MakeDisplayPath(fname, 50);
#if USE_BOOST_FORMAT
boost::format fmt(_("Could not save the document\n%1$s\ndue to the error: %2$s"));
fmt % file;
fmt % error;
string text = fmt.str();
#else
string text = _("Could not save the document\n");
text += file + _(" due to the error: ");
text += error;
#endif
Alert::error(_("Could not save document"), text);
return;
}
writeFileAscii(ofs, linelen);
@ -982,7 +1001,19 @@ void Buffer::makeLaTeXFile(string const & fname,
ofstream ofs(fname.c_str());
if (!ofs) {
Alert::err_alert(_("Error: Cannot open file: "), fname);
string const error = strerror(errno);
string const file = MakeDisplayPath(fname, 50);
#if USE_BOOST_FORMAT
boost::format fmt(_("Could not open the specified document\n%1$s\ndue to the error: %2$s"));
fmt % file;
fmt % error;
string text = fmt.str();
#else
string text = _("Could not open the specified document\n");
text += file + _(" due to the error: ");
text += error;
#endif
Alert::error(_("Could not open file"), text);
return;
}

View File

@ -17,17 +17,10 @@
#include "Alert_pimpl.h"
#include <cerrno>
#ifndef CXX_GLOBAL_CSTD
using std::strerror;
#endif
using std::endl;
using std::pair;
using std::make_pair;
void Alert::alert(string const & s1, string const & s2, string const & s3)
{
if (!lyxrc.use_gui) {
@ -40,12 +33,6 @@ void Alert::alert(string const & s1, string const & s2, string const & s3)
}
void Alert::err_alert(string const & s1, string const & s2)
{
alert(s1, s2, strerror(errno));
}
int Alert::prompt(string const & title, string const & question,
int default_button,
string const & b1, string const & b2, string const & b3)
@ -66,6 +53,39 @@ int Alert::prompt(string const & title, string const & question,
}
void Alert::warning(string const & title, string const & message)
{
if (lyxrc.use_gui)
return warning_pimpl(title, message);
lyxerr << "Warning: " << title << endl;
lyxerr << "----------------------------------------" << endl;
lyxerr << message << endl;
}
void Alert::error(string const & title, string const & message)
{
if (lyxrc.use_gui)
return error_pimpl(title, message);
lyxerr << "Error: " << title << endl;
lyxerr << "----------------------------------------" << endl;
lyxerr << message << endl;
}
void Alert::information(string const & title, string const & message)
{
if (lyxrc.use_gui)
return information_pimpl(title, message);
lyxerr << title << endl;
lyxerr << "----------------------------------------" << endl;
lyxerr << message << endl;
}
pair<bool, string> const Alert::askForText(string const & msg,
string const & dflt)
{

View File

@ -32,14 +32,31 @@ int prompt(string const & title, string const & question,
int default_button,
string const & b1, string const & b2, string const & b3 = string());
/// show an alert message
/**
* Display a warning to the user. Title should be a short summary.
* Only use this if the user cannot perform some remedial action.
*/
void warning(string const & title, string const & message);
/**
* Display a warning to the user. Title should be a short summary.
* Only use this if the user cannot perform some remedial action.
*/
void error(string const & title, string const & message);
/**
* Informational message. Use very very sparingly. That is, you must
* apply to me, in triplicate, under the sea, breathing in petrol
* and reciting the Nicene Creed, whilst running uphill and also
* eating.
*/
void information(string const & title, string const & message);
/// show an alert message. DO NOT USE !!
void alert(string const & title, string const & s1 = string(),
string const & s2 = string());
/// show an alert message and strerror(errno)
void err_alert(string const & s1, string const & s2 = string());
/// Asks for a text
/// Asks for a text. DO NOT USE !!
std::pair<bool, string> const
askForText(string const & msg,
string const & dflt = string());

View File

@ -18,4 +18,8 @@ int prompt_pimpl(string const & title, string const & question,
int default_button,
string const & b1, string const & b2, string const & b3);
void warning_pimpl(string const & title, string const & warning);
void error_pimpl(string const & title, string const & warning);
void information_pimpl(string const & title, string const & warning);
std::pair<bool, string> const askForText_pimpl(string const & msg, string const & dflt);

View File

@ -1,3 +1,10 @@
2003-03-29 John Levon <levon@movementarian.org>
* Alert.h:
* Alert.C:
* Alert_pimpl.h: kill err_alert. Add information(),
warning(), error()
2003-03-29 John Levon <levon@movementarian.org>
* Alert.h:

View File

@ -55,6 +55,45 @@ int prompt_pimpl(string const & tit, string const & question,
}
void warning_pimpl(string const & tit, string const & message)
{
#if USE_BOOST_FORMAT
boost::format fmt(_("LyX: %1$s"));
fmt % tit;
string const title = fmt.str();
#else
string const title = _("LyX: ") + tit;
#endif
QMessageBox::warning(0, toqstr(title), toqstr(formatted(message)));
}
void error_pimpl(string const & tit, string const & message)
{
#if USE_BOOST_FORMAT
boost::format fmt(_("LyX: %1$s"));
fmt % tit;
string const title = fmt.str();
#else
string const title = _("LyX: ") + tit;
#endif
QMessageBox::critical(0, toqstr(title), toqstr(formatted(message)));
}
void information_pimpl(string const & tit, string const & message)
{
#if USE_BOOST_FORMAT
boost::format fmt(_("LyX: %1$s"));
fmt % tit;
string const title = fmt.str();
#else
string const title = _("LyX: ") + tit;
#endif
QMessageBox::information(0, toqstr(title), toqstr(formatted(message)));
}
pair<bool, string> const
askForText_pimpl(string const & msg, string const & dflt)
{

View File

@ -1,3 +1,7 @@
2003-03-29 John Levon <levon@movementarian.org>
* Alert_pimpl.C: implement warning(), information(), error()
2003-03-29 John Levon <levon@movementarian.org>
* Alert_pimpl.C: implement prompt()

View File

@ -32,6 +32,24 @@ void alert_pimpl(string const & s1, string const & s2, string const & s3)
}
void warning_pimpl(string const &, string const & message)
{
fl_show_messages(message.c_str());
}
void error_pimpl(string const &, string const & message)
{
fl_show_messages(message.c_str());
}
void information_pimpl(string const &, string const & message)
{
fl_show_messages(message.c_str());
}
int prompt_pimpl(string const &, string const & question,
int default_button,
string const & b1, string const & b2, string const & b3)

View File

@ -1,3 +1,7 @@
2003-03-29 John Levon <levon@movementarian.org>
* Alert_pimpl.C: implement information(), warning(), error()
2003-03-29 John Levon <levon@movementarian.org>
* xforms_helpers.h:

View File

@ -188,8 +188,11 @@ void FileDialog::Private::Reread()
// Opens directory
DIR * dir = ::opendir(directory_.c_str());
if (!dir) {
// FIXME: re-add ...
#if 0
Alert::err_alert(_("Warning! Couldn't open directory."),
directory_);
#endif
directory_ = lyx::getcwd();
dir = ::opendir(directory_.c_str());
}
@ -357,7 +360,10 @@ void FileDialog::Private::SetDirectory(string const & path)
// must check the directory exists
DIR * dir = ::opendir(tmp.c_str());
if (!dir) {
// FIXME: re-add ...
#if 0
Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
#endif
} else {
::closedir(dir);
directory_ = tmp;

View File

@ -229,7 +229,16 @@ void QuitLyX()
// do any other cleanup procedures now
lyxerr[Debug::INFO] << "Deleting tmp dir " << system_tempdir << endl;
DestroyLyXTmpDir(system_tempdir);
if (destroyDir(system_tempdir) != 0) {
#if USE_BOOST_FORMAT
boost::format fmt = _("Could not remove the temporary directory %1$s");
fmt % system_tempdir;
string msg = fmt.str();
#else
string msg = _("Could not remove the temporary directory ") + system_tempdir;
#endif
Alert::warning(_("Could not remove temporary directory"), msg);
}
lyx_gui::exit();
}
@ -426,15 +435,37 @@ string getContentsOfAsciiFile(BufferView * bv, string const & f, bool asParagrap
FileInfo fi(fname);
if (!fi.readable()) {
Alert::err_alert(_("Error! Specified file is unreadable: "),
MakeDisplayPath(fname, 50));
string const error = strerror(errno);
string const file = MakeDisplayPath(fname, 50);
#if USE_BOOST_FORMAT
boost::format fmt(_("Could not read the specified document\n%1$s\ndue to the error: %2$s"));
fmt % file;
fmt % error;
string text = fmt.str();
#else
string text = _("Could not read the specified document\n");
text += file + _(" due to the error: ");
text += error;
#endif
Alert::error(_("Could not read file"), text);
return string();
}
ifstream ifs(fname.c_str());
if (!ifs) {
Alert::err_alert(_("Error! Cannot open specified file:"),
MakeDisplayPath(fname, 50));
string const error = strerror(errno);
string const file = MakeDisplayPath(fname, 50);
#if USE_BOOST_FORMAT
boost::format fmt(_("Could not open the specified document\n%1$s\ndue to the error: %2$s"));
fmt % file;
fmt % error;
string text = fmt.str();
#else
string text = _("Could not open the specified document\n");
text += file + _(" due to the error: ");
text += error;
#endif
Alert::error(_("Could not open file"), text);
return string();
}
@ -513,7 +544,9 @@ void Reconfigure(BufferView * bv)
p.pop();
bv->owner()->message(_("Reloading configuration..."));
lyxrc.read(LibFileSearch(string(), "lyxrc.defaults"));
Alert::alert(_("The system has been reconfigured."),
_("You need to restart LyX to make use of any"),
_("updated document class specifications."));
Alert::information(_("System reconfigured"),
_("The system has been reconfigured.\n"
"You need to restart LyX to make use of any \n"
"updated document class specifications."));
}

View File

@ -1,3 +1,8 @@
2003-03-29 John Levon <levon@movementarian.org>
* filetools.h:
* filetools.C: never call Alert directly from here
2003-03-12 John Levon <levon@movementarian.org>
* textutils.h: remove META_NEWLINE

View File

@ -21,10 +21,10 @@
#include "debug.h"
#include "support/lstrings.h"
#include "support/systemcall.h"
#include "support/LAssert.h"
#include "filetools.h"
#include "lstrings.h"
#include "frontends/Alert.h"
#include "FileInfo.h"
#include "support/path.h" // I know it's OS/2 specific (SMiyata)
#include "gettext.h"
@ -379,7 +379,6 @@ int DeleteAllFilesInDir(string const & path)
// directory_iterator dit(path);
// directory_iterator dend;
// if (dit == dend) {
// Alert::err_alert(_("Error! Cannot open directory:"), path);
// return -1;
// }
// for (; dit != dend; ++dit) {
@ -387,16 +386,13 @@ int DeleteAllFilesInDir(string const & path)
// if (filename == "." || filename == "..")
// continue;
// string unlinkpath(AddName(path, filename));
// if (lyx::unlink(unlinkpath))
// Alert::err_alert(_("Error! Could not remove file:"),
// unlinkpath);
// lyx::unlink(unlinkpath);
// }
// return 0;
DIR * dir = ::opendir(path.c_str());
if (!dir) {
Alert::err_alert (_("Error! Cannot open directory:"), path);
if (!dir)
return -1;
}
struct dirent * de;
int return_value = 0;
while ((de = readdir(dir))) {
@ -413,11 +409,8 @@ int DeleteAllFilesInDir(string const & path)
if (fi.isOK() && fi.isDir())
deleted = (DeleteAllFilesInDir(unlinkpath) == 0);
deleted &= (lyx::unlink(unlinkpath) == 0);
if (!deleted) {
Alert::err_alert(_("Error! Could not remove file:"),
unlinkpath);
if (!deleted)
return_value = -1;
}
}
closedir(dir);
return return_value;
@ -437,33 +430,29 @@ string const CreateTmpDir(string const & tempdir, string const & mask)
// safe because of the gap between unlink and mkdir. (Lgb)
lyx::unlink(tmpfl.c_str());
if (tmpfl.empty() || lyx::mkdir(tmpfl, 0700)) {
Alert::err_alert(_("Error! Couldn't create temporary directory:"),
tempdir);
if (tmpfl.empty() || lyx::mkdir(tmpfl, 0700))
return string();
}
return MakeAbsPath(tmpfl);
}
} // namespace anon
int DestroyTmpDir(string const & tmpdir, bool Allfiles)
int destroyDir(string const & tmpdir)
{
#ifdef __EMX__
Path p(user_lyxdir);
#endif
if (Allfiles && DeleteAllFilesInDir(tmpdir)) {
if (DeleteAllFilesInDir(tmpdir))
return -1;
}
if (lyx::rmdir(tmpdir)) {
Alert::err_alert(_("Error! Couldn't delete temporary directory:"),
tmpdir);
if (lyx::rmdir(tmpdir))
return -1;
}
return 0;
}
} // namespace anon
string const CreateBufferTmpDir(string const & pathfor)
{
@ -474,20 +463,12 @@ string const CreateBufferTmpDir(string const & pathfor)
// of EMX mkstemp().
string const tmpfl = tmpdir + "/lyx_tmpbuf" + tostr(count++);
if (lyx::mkdir(tmpfl, 0777)) {
Alert::err_alert(_("Error! Couldn't create temporary directory:"),
tmpdir);
return string();
}
return tmpfl;
}
int DestroyBufferTmpDir(string const & tmpdir)
{
return DestroyTmpDir(tmpdir, true);
}
string const CreateLyXTmpDir(string const & deflt)
{
if ((!deflt.empty()) && (deflt != "/tmp")) {
@ -507,28 +488,15 @@ string const CreateLyXTmpDir(string const & deflt)
}
// FIXME: no need for separate method like this ...
int DestroyLyXTmpDir(string const & tmpdir)
{
return DestroyTmpDir(tmpdir, true);
}
// Creates directory. Returns true if succesfull
bool createDirectory(string const & path, int permission)
{
string temp(rtrim(os::slashify_path(path), "/"));
if (temp.empty()) {
Alert::alert(_("Internal error!"),
_("Call to createDirectory with invalid name"));
return false;
}
lyx::Assert(!temp.empty());
if (lyx::mkdir(temp, permission)) {
Alert::err_alert (_("Error! Couldn't create directory:"), temp);
if (lyx::mkdir(temp, permission))
return false;
}
return true;
}
@ -1303,11 +1271,8 @@ void removeAutosaveFile(string const & filename)
a += OnlyFilename(filename);
a += '#';
FileInfo const fileinfo(a);
if (fileinfo.exist()) {
if (lyx::unlink(a) != 0) {
Alert::err_alert(_("Could not delete auto-save file!"), a);
}
}
if (fileinfo.exist())
lyx::unlink(a);
}

View File

@ -13,21 +13,18 @@
#include "LString.h"
/// remove directory and all contents, returns 0 on success
int destroyDir(string const & tmpdir);
///
string const CreateBufferTmpDir(string const & pathfor = string());
/// Creates directory. Returns true on succes.
/// Creates directory. Returns true on success
bool createDirectory(string const & name, int permissions);
///
string const CreateLyXTmpDir(string const & deflt);
///
int DestroyBufferTmpDir(string const & tmpdir);
///
int DestroyLyXTmpDir(string const & tmpdir);
/** Find file by searching several directories.
Uses a string of paths separated by ";"s to find a file to open.
Can't cope with pathnames with a ';' in them. Returns full path to file.