Display error/warning dialogs if possible.

* ExceptionMessage: new exception based message for errors and warnings.

* package.C.in:
  - replace lyxerr based errors and warning with ExceptionMessage throwing.
  - bail_out(): erased!

* os_win32.C:
  - replace lyxerr based errors and warning with ExceptionMessage throwing.
  - bail_out(): erased!

* tex2lyx.C: catch any exception from Package class.

* lyx_main.C:  catch any exception from Package class and act accordingly.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16834 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-01-24 15:49:20 +00:00
parent 9281cd4675
commit 801f807063
7 changed files with 96 additions and 51 deletions

View File

@ -99,6 +99,7 @@ src_support_header_files = Split('''
docstream.h
docstring.h
environment.h
ExceptionMessage.h
filefilterlist.h
filename.h
filetools.h

View File

@ -53,6 +53,7 @@
#include "support/filetools.h"
#include "support/lyxlib.h"
#include "support/convert.h"
#include "support/ExceptionMessage.h"
#include "support/os.h"
#include "support/package.h"
#include "support/path.h"
@ -395,9 +396,17 @@ int LyX::exec(int & argc, char * argv[])
// we need to parse for "-dbg" and "-help"
easyParse(argc, argv);
support::init_package(to_utf8(from_local8bit(argv[0])),
try { support::init_package(to_utf8(from_local8bit(argv[0])),
cl_system_support, cl_user_support,
support::top_build_dir_is_one_level_up);
} catch (support::ExceptionMessage const & message) {
if (message.type_ == support::ErrorException) {
Alert::error(message.title_, message.details_);
exit(1);
} else if (message.type_ == support::WarningException) {
Alert::warning(message.title_, message.details_);
}
}
if (!use_gui) {
// FIXME: create a ConsoleApplication

View File

@ -0,0 +1,49 @@
// -*- C++ -*-
/**
* \file ExceptionMessage.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*
* A store of the paths to the various different directoies used
* by LyX. These paths differ markedly from one OS to another,
* following the local Windows, MacOS X or Posix conventions.
*/
#ifndef LYX_MESSAGE_H
#define LYX_MESSAGE_H
#include "support/docstring.h"
#include <exception>
namespace lyx {
namespace support {
enum ExceptionType {
ErrorException,
WarningException
};
class ExceptionMessage: public std::exception {
public:
ExceptionMessage(ExceptionType type, docstring const & title,
docstring const & details)
: exception((to_utf8(title) + "\n" + to_utf8(details)).c_str()),
type_(type), title_(title), details_(details) {}
virtual ~ExceptionMessage() {}
ExceptionType type_;
docstring title_;
docstring details_;
};
} // namespace support
} // namespace lyx
#endif // LYX_MESSAGE_H

View File

@ -35,6 +35,7 @@ libsupport_la_SOURCES = \
docstring.h \
environment.h \
environment.C \
ExceptionMessage.h \
filefilterlist.C \
filefilterlist.h \
filename.C \

View File

@ -18,10 +18,12 @@
#include "support/os_win32.h"
#include "support/lstrings.h"
#include "support/filetools.h"
#include "support/ExceptionMessage.h"
#include "support/package.h"
#include "support/path.h"
#include "debug.h"
#include "gettext.h"
#include <boost/assert.hpp>
@ -324,36 +326,21 @@ void windows_style_tex_paths(bool use_windows_paths)
}
namespace {
void bail_out()
{
#ifndef CXX_GLOBAL_CSTD
using std::exit;
#endif
exit(1);
}
} // namespace anon
GetFolderPath::GetFolderPath()
: folder_module_(0),
folder_path_func_(0)
{
folder_module_ = LoadLibrary("shfolder.dll");
if (!folder_module_) {
lyxerr << "Unable to load shfolder.dll\nPlease install."
<< std::endl;
bail_out();
throw ExceptionMessage(ErrorException, _("System file not found"),
_("Unable to load shfolder.dll\nPlease install."));
}
folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA"));
if (folder_path_func_ == 0) {
lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n"
"Don't know how to proceed. Sorry."
<< std::endl;
bail_out();
throw ExceptionMessage(ErrorException, _("System function not found"),
_("Unable to find SHGetFolderPathA in shfolder.dll\n"
"Don't know how to proceed. Sorry."));
}
}

View File

@ -22,6 +22,7 @@
#include "support/environment.h"
#include "support/filetools.h"
#include "support/lstrings.h"
#include "support/ExceptionMessage.h"
#include "support/os.h"
#if defined (USE_WINDOWS_PACKAGING)
@ -369,15 +370,6 @@ string const get_temp_dir()
}
void bail_out()
{
#ifndef CXX_GLOBAL_CSTD
using std::exit;
#endif
exit(1);
}
// Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
string const abs_path_from_command_line(string const & command_line)
{
@ -440,11 +432,10 @@ string const abs_path_from_binary_name(string const & exe)
string const abs_binary = get_binary_path(exe);
if (abs_binary.empty()) {
// FIXME UNICODE
lyxerr << lyx::to_utf8(bformat(_("Unable to determine the path to the "
"LyX binary from the command line %1$s"),
lyx::from_utf8(exe)))
<< std::endl;
bail_out();
throw ExceptionMessage(ErrorException,
_("LyX binary not found"),
bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
lyx::from_utf8(exe)));
}
return abs_binary;
}
@ -562,17 +553,16 @@ get_system_support_dir(string const & abs_binary,
}
// FIXME UNICODE
lyxerr << lyx::to_utf8(bformat(_("Unable to determine the system directory "
throw ExceptionMessage(ErrorException, _("No system directory"),
bformat(_("Unable to determine the system directory "
"having searched\n"
"\t%1$s\n"
"Use the '-sysdir' command line parameter or "
"set the environment variable LYX_DIR_15x to "
"the LyX system directory containing the file "
"`chkconfig.ltx'."),
lyx::from_utf8(searched_dirs_str)))
<< std::endl;
lyx::from_utf8(searched_dirs_str)));
bail_out();
// Keep the compiler happy.
return string();
}
@ -648,11 +638,10 @@ bool check_command_line_dir(string const & dir,
FileName const abs_path = fileSearch(dir, file);
if (abs_path.empty()) {
// FIXME UNICODE
lyxerr << lyx::to_utf8(bformat(_("Invalid %1$s switch.\n"
"Directory %2$s does not contain %3$s."),
throw ExceptionMessage(WarningException, _("File not found"), bformat(
_("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
lyx::from_utf8(command_line_switch), lyx::from_utf8(dir),
lyx::from_utf8(file)))
<< std::endl;
lyx::from_utf8(file)));
}
return !abs_path.empty();
@ -676,10 +665,11 @@ bool check_env_var_dir(string const & dir,
FileName const abs_path = fileSearch(dir, file);
if (abs_path.empty()) {
// FIXME UNICODE
lyxerr << lyx::to_utf8(bformat(_("Invalid %1$s environment variable.\n"
throw ExceptionMessage(WarningException, _("File not found"), bformat(
_("Invalid %1$s environment variable.\n"
"Directory %2$s does not contain %3$s."),
lyx::from_utf8(env_var), lyx::from_utf8(dir), lyx::from_utf8(file)))
<< std::endl;
lyx::from_utf8(env_var), lyx::from_utf8(dir),
lyx::from_utf8(file)));
}
return !abs_path.empty();
@ -703,8 +693,8 @@ bool check_env_var_dir(string const & dir,
docstring const fmt =
_("Invalid %1$s environment variable.\n%2$s is not a directory.");
lyxerr << lyx::to_utf8(bformat(fmt, lyx::from_utf8(env_var), lyx::from_utf8(dir)))
<< std::endl;
throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
fmt, lyx::from_utf8(env_var), lyx::from_utf8(dir)));
}
return success;

View File

@ -23,6 +23,7 @@
#include "support/fs_extras.h"
#include "support/lstrings.h"
#include "support/lyxlib.h"
#include "support/ExceptionMessage.h"
#include "support/os.h"
#include "support/package.h"
#include "support/unicode.h"
@ -518,9 +519,16 @@ int main(int argc, char * argv[])
}
lyx::support::os::init(argc, argv);
support::init_package(to_utf8(from_local8bit(argv[0])),
try { support::init_package(to_utf8(from_local8bit(argv[0])),
cl_system_support, cl_user_support,
support::top_build_dir_is_two_levels_up);
} catch (support::ExceptionMessage const & message) {
cerr << to_utf8(message.title_) << ':\n'
<< to_utf8(message.details_) << endl;
if (message.type_ == support::ErrorException)
exit(1);
}
// Now every known option is parsed. Look for input and output
// file name (the latter is optional).