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 docstream.h
docstring.h docstring.h
environment.h environment.h
ExceptionMessage.h
filefilterlist.h filefilterlist.h
filename.h filename.h
filetools.h filetools.h

View File

@ -53,6 +53,7 @@
#include "support/filetools.h" #include "support/filetools.h"
#include "support/lyxlib.h" #include "support/lyxlib.h"
#include "support/convert.h" #include "support/convert.h"
#include "support/ExceptionMessage.h"
#include "support/os.h" #include "support/os.h"
#include "support/package.h" #include "support/package.h"
#include "support/path.h" #include "support/path.h"
@ -395,9 +396,17 @@ int LyX::exec(int & argc, char * argv[])
// we need to parse for "-dbg" and "-help" // we need to parse for "-dbg" and "-help"
easyParse(argc, argv); 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, cl_system_support, cl_user_support,
support::top_build_dir_is_one_level_up); 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) { if (!use_gui) {
// FIXME: create a ConsoleApplication // 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 \ docstring.h \
environment.h \ environment.h \
environment.C \ environment.C \
ExceptionMessage.h \
filefilterlist.C \ filefilterlist.C \
filefilterlist.h \ filefilterlist.h \
filename.C \ filename.C \

View File

@ -18,10 +18,12 @@
#include "support/os_win32.h" #include "support/os_win32.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/filetools.h" #include "support/filetools.h"
#include "support/ExceptionMessage.h"
#include "support/package.h" #include "support/package.h"
#include "support/path.h" #include "support/path.h"
#include "debug.h" #include "debug.h"
#include "gettext.h"
#include <boost/assert.hpp> #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() GetFolderPath::GetFolderPath()
: folder_module_(0), : folder_module_(0),
folder_path_func_(0) folder_path_func_(0)
{ {
folder_module_ = LoadLibrary("shfolder.dll"); folder_module_ = LoadLibrary("shfolder.dll");
if (!folder_module_) { if (!folder_module_) {
lyxerr << "Unable to load shfolder.dll\nPlease install." throw ExceptionMessage(ErrorException, _("System file not found"),
<< std::endl; _("Unable to load shfolder.dll\nPlease install."));
bail_out();
} }
folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA")); folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA"));
if (folder_path_func_ == 0) { if (folder_path_func_ == 0) {
lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n" throw ExceptionMessage(ErrorException, _("System function not found"),
"Don't know how to proceed. Sorry." _("Unable to find SHGetFolderPathA in shfolder.dll\n"
<< std::endl; "Don't know how to proceed. Sorry."));
bail_out();
} }
} }

View File

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

View File

@ -23,6 +23,7 @@
#include "support/fs_extras.h" #include "support/fs_extras.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/lyxlib.h" #include "support/lyxlib.h"
#include "support/ExceptionMessage.h"
#include "support/os.h" #include "support/os.h"
#include "support/package.h" #include "support/package.h"
#include "support/unicode.h" #include "support/unicode.h"
@ -518,9 +519,16 @@ int main(int argc, char * argv[])
} }
lyx::support::os::init(argc, 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, cl_system_support, cl_user_support,
support::top_build_dir_is_two_levels_up); 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 // Now every known option is parsed. Look for input and output
// file name (the latter is optional). // file name (the latter is optional).