From: John Levon Date: Sat, 29 Mar 2003 09:02:08 +0000 (+0000) Subject: more Alert:: work X-Git-Tag: 1.6.10~17132 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=20215c83e735b9cbbd3b075067e389dcad1b2d84;p=features.git more Alert:: work git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6625 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/BufferView.C b/src/BufferView.C index f78eab403d..c0656c2631 100644 --- a/src/BufferView.C +++ b/src/BufferView.C @@ -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 @@ -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; } diff --git a/src/ChangeLog b/src/ChangeLog index 3405e11785..2dbf519ca2 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2003-03-29 John Levon + + * lyx_cb.C: + * BufferView.C: + * buffer.C: warnings pushed down from support/, + kill err_alert + 2003-03-29 John Levon * lyxfunc.C: safety check for C-r (revert) diff --git a/src/buffer.C b/src/buffer.C index d0f4436673..486bb4c140 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -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; } diff --git a/src/frontends/Alert.C b/src/frontends/Alert.C index 334e8d103b..5b39e2e5bc 100644 --- a/src/frontends/Alert.C +++ b/src/frontends/Alert.C @@ -17,17 +17,10 @@ #include "Alert_pimpl.h" -#include - -#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 const Alert::askForText(string const & msg, string const & dflt) { diff --git a/src/frontends/Alert.h b/src/frontends/Alert.h index 0e235546a4..ead44a879d 100644 --- a/src/frontends/Alert.h +++ b/src/frontends/Alert.h @@ -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 const askForText(string const & msg, string const & dflt = string()); diff --git a/src/frontends/Alert_pimpl.h b/src/frontends/Alert_pimpl.h index b29d1fd9e0..f9c587d587 100644 --- a/src/frontends/Alert_pimpl.h +++ b/src/frontends/Alert_pimpl.h @@ -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 const askForText_pimpl(string const & msg, string const & dflt); diff --git a/src/frontends/ChangeLog b/src/frontends/ChangeLog index bc3802240e..9ecdbe6530 100644 --- a/src/frontends/ChangeLog +++ b/src/frontends/ChangeLog @@ -1,3 +1,10 @@ +2003-03-29 John Levon + + * Alert.h: + * Alert.C: + * Alert_pimpl.h: kill err_alert. Add information(), + warning(), error() + 2003-03-29 John Levon * Alert.h: diff --git a/src/frontends/qt2/Alert_pimpl.C b/src/frontends/qt2/Alert_pimpl.C index b63b580585..ad38db003f 100644 --- a/src/frontends/qt2/Alert_pimpl.C +++ b/src/frontends/qt2/Alert_pimpl.C @@ -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 const askForText_pimpl(string const & msg, string const & dflt) { diff --git a/src/frontends/qt2/ChangeLog b/src/frontends/qt2/ChangeLog index 5038e8191c..df5f88d854 100644 --- a/src/frontends/qt2/ChangeLog +++ b/src/frontends/qt2/ChangeLog @@ -1,3 +1,7 @@ +2003-03-29 John Levon + + * Alert_pimpl.C: implement warning(), information(), error() + 2003-03-29 John Levon * Alert_pimpl.C: implement prompt() diff --git a/src/frontends/xforms/Alert_pimpl.C b/src/frontends/xforms/Alert_pimpl.C index eed7b52495..298b4dfc1a 100644 --- a/src/frontends/xforms/Alert_pimpl.C +++ b/src/frontends/xforms/Alert_pimpl.C @@ -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) diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 0e95aa1591..65103fcf78 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,7 @@ +2003-03-29 John Levon + + * Alert_pimpl.C: implement information(), warning(), error() + 2003-03-29 John Levon * xforms_helpers.h: diff --git a/src/frontends/xforms/FormFiledialog.C b/src/frontends/xforms/FormFiledialog.C index 0e9120de3e..a8fe2ec9c9 100644 --- a/src/frontends/xforms/FormFiledialog.C +++ b/src/frontends/xforms/FormFiledialog.C @@ -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; diff --git a/src/lyx_cb.C b/src/lyx_cb.C index f47700b01b..4230954a60 100644 --- a/src/lyx_cb.C +++ b/src/lyx_cb.C @@ -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.")); } diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 1b018abfa2..8d847b017f 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,8 @@ +2003-03-29 John Levon + + * filetools.h: + * filetools.C: never call Alert directly from here + 2003-03-12 John Levon * textutils.h: remove META_NEWLINE diff --git a/src/support/filetools.C b/src/support/filetools.C index 5a2f3f3dab..51ac03f9d0 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -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); } diff --git a/src/support/filetools.h b/src/support/filetools.h index 35f4badfc9..21e09e47d6 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -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.