From: André Pönitz Date: Thu, 25 Oct 2007 06:09:38 +0000 (+0000) Subject: dissolve Importer 'class' to LyXFunc/Converter X-Git-Tag: 1.6.10~7669 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=93bd28f0404cfedfa912be4892878692775b8d9c;p=features.git dissolve Importer 'class' to LyXFunc/Converter git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21195 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/Converter.cpp b/src/Converter.cpp index 481d706ed1..b168cd1a24 100644 --- a/src/Converter.cpp +++ b/src/Converter.cpp @@ -701,11 +701,34 @@ bool Converters::isReachable(string const & from, string const & to) } -Graph::EdgePath const -Converters::getPath(string const & from, string const & to) +Graph::EdgePath Converters::getPath(string const & from, string const & to) { return G_.getPath(formats.getNumber(from), formats.getNumber(to)); } + +vector Converters::importableFormats() +{ + vector l = loaders(); + vector result = getReachableTo(l[0], true); + for (vector::const_iterator it = l.begin() + 1; + it != l.end(); ++it) { + vector r = getReachableTo(*it, false); + result.insert(result.end(), r.begin(), r.end()); + } + return result; +} + + +vector Converters::loaders() const +{ + vector v; + v.push_back("lyx"); + v.push_back("text"); + v.push_back("textparagraph"); + return v; +} + + } // namespace lyx diff --git a/src/Converter.h b/src/Converter.h index 77b5e363b5..942f33acd1 100644 --- a/src/Converter.h +++ b/src/Converter.h @@ -98,10 +98,15 @@ public: std::vector const getReachable(std::string const & from, bool only_viewable, bool clear_visited); + + std::vector importableFormats(); + + std::vector loaders() const; + /// Does a conversion path from format \p from to format \p to exist? bool isReachable(std::string const & from, std::string const & to); /// - Graph::EdgePath const getPath(std::string const & from, std::string const & to); + Graph::EdgePath getPath(std::string const & from, std::string const & to); /// OutputParams::FLAVOR getFlavor(Graph::EdgePath const & path); /// Flags for converting files diff --git a/src/Importer.cpp b/src/Importer.cpp deleted file mode 100644 index 902df65e1a..0000000000 --- a/src/Importer.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/** - * \file Importer.cpp - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author unknown - * \author Lars Gullik Bjønnes - * \author Jean-Marc Lasgouttes - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "Importer.h" -#include "Converter.h" -#include "Format.h" -#include "frontends/LyXView.h" -#include "FuncRequest.h" - -#include "support/filetools.h" - -#include "frontends/alert.h" - -#include "gettext.h" -#include "BufferView.h" -#include "buffer_funcs.h" - -using std::find; -using std::string; -using std::vector; - - -namespace lyx { - -using support::bformat; -using support::changeExtension; -using support::FileName; -using support::makeDisplayPath; -using frontend::LyXView; - - -bool Importer::Import(LyXView * lv, FileName const & filename, - string const & format, ErrorList & errorList) -{ - docstring const displaypath = makeDisplayPath(filename.absFilename()); - lv->message(bformat(_("Importing %1$s..."), displaypath)); - - FileName const lyxfile(changeExtension(filename.absFilename(), ".lyx")); - - string loader_format; - vector loaders = Loaders(); - if (find(loaders.begin(), loaders.end(), format) == loaders.end()) { - for (vector::const_iterator it = loaders.begin(); - it != loaders.end(); ++it) { - if (theConverters().isReachable(format, *it)) { - string const tofile = - changeExtension(filename.absFilename(), - formats.extension(*it)); - if (!theConverters().convert(0, filename, FileName(tofile), - filename, format, *it, errorList)) - return false; - loader_format = *it; - break; - } - } - if (loader_format.empty()) { - frontend::Alert::error(_("Couldn't import file"), - bformat(_("No information for importing the format %1$s."), - formats.prettyName(format))); - return false; - } - } else { - loader_format = format; - } - - - if (loader_format == "lyx") { - Buffer * buf = lv->loadLyXFile(lyxfile); - if (!buf) { - // we are done - lv->message(_("file not imported!")); - return false; - } - updateLabels(*buf); - lv->setBuffer(buf); - lv->showErrorList("Parse"); - } else { - Buffer * const b = newFile(lyxfile.absFilename(), string(), true); - if (b) - lv->setBuffer(b); - else - return false; - bool as_paragraphs = loader_format == "textparagraph"; - string filename2 = (loader_format == format) ? filename.absFilename() - : changeExtension(filename.absFilename(), - formats.extension(loader_format)); - lv->view()->insertPlaintextFile(filename2, as_paragraphs); - lv->dispatch(FuncRequest(LFUN_MARK_OFF)); - } - - // we are done - lv->message(_("imported.")); - return true; -} - - -vector const Importer::GetImportableFormats() -{ - vector loaders = Loaders(); - vector result = - theConverters().getReachableTo(loaders[0], true); - for (vector::const_iterator it = loaders.begin() + 1; - it != loaders.end(); ++it) { - vector r = - theConverters().getReachableTo(*it, false); - result.insert(result.end(), r.begin(), r.end()); - } - return result; -} - - -vector const Importer::Loaders() -{ - vector v; - v.push_back("lyx"); - v.push_back("text"); - v.push_back("textparagraph"); - return v; -} - - -} // namespace lyx diff --git a/src/Importer.h b/src/Importer.h deleted file mode 100644 index 4483d7825f..0000000000 --- a/src/Importer.h +++ /dev/null @@ -1,47 +0,0 @@ -// -*- C++ -*- -/** - * \file Importer.h - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author unknown - * \author Jean-Marc Lasgouttes - * \author John Levon - * - * Full author contact details are available in file CREDITS. - */ - -#ifndef IMPORTER_H -#define IMPORTER_H - -#include -#include - - -namespace lyx { - -namespace support { class FileName; } - -class ErrorList; -class Format; - -namespace frontend { -class LyXView; -} - -class Importer { -public: - /// - static bool Import(frontend::LyXView * lv, support::FileName const & filename, - std::string const & format, ErrorList & errorList); - - /// - static std::vector const GetImportableFormats(); -private: - /// - static std::vector const Loaders(); -}; - -} // namespace lyx - -#endif diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index 541113b589..87e6b031e4 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -30,6 +30,7 @@ #include "BufferView.h" #include "CmdDef.h" #include "Color.h" +#include "Converter.h" #include "Cursor.h" #include "CutAndPaste.h" #include "debug.h" @@ -40,7 +41,6 @@ #include "FuncRequest.h" #include "FuncStatus.h" #include "gettext.h" -#include "Importer.h" #include "InsetIterator.h" #include "Intl.h" #include "KeyMap.h" @@ -107,6 +107,8 @@ using std::pair; using std::string; using std::istringstream; using std::ostringstream; +using std::find; +using std::vector; namespace fs = boost::filesystem; @@ -136,12 +138,80 @@ using support::token; using support::trim; using support::prefixIs; + namespace Alert = frontend::Alert; extern bool quitting; namespace { + +bool import(LyXView * lv, FileName const & filename, + string const & format, ErrorList & errorList) +{ + docstring const displaypath = makeDisplayPath(filename.absFilename()); + lv->message(bformat(_("Importing %1$s..."), displaypath)); + + FileName const lyxfile(changeExtension(filename.absFilename(), ".lyx")); + + string loader_format; + vector loaders = theConverters().loaders(); + if (find(loaders.begin(), loaders.end(), format) == loaders.end()) { + for (vector::const_iterator it = loaders.begin(); + it != loaders.end(); ++it) { + if (theConverters().isReachable(format, *it)) { + string const tofile = + changeExtension(filename.absFilename(), + formats.extension(*it)); + if (!theConverters().convert(0, filename, FileName(tofile), + filename, format, *it, errorList)) + return false; + loader_format = *it; + break; + } + } + if (loader_format.empty()) { + frontend::Alert::error(_("Couldn't import file"), + bformat(_("No information for importing the format %1$s."), + formats.prettyName(format))); + return false; + } + } else { + loader_format = format; + } + + + if (loader_format == "lyx") { + Buffer * buf = lv->loadLyXFile(lyxfile); + if (!buf) { + // we are done + lv->message(_("file not imported!")); + return false; + } + updateLabels(*buf); + lv->setBuffer(buf); + lv->showErrorList("Parse"); + } else { + Buffer * const b = newFile(lyxfile.absFilename(), string(), true); + if (b) + lv->setBuffer(b); + else + return false; + bool as_paragraphs = loader_format == "textparagraph"; + string filename2 = (loader_format == format) ? filename.absFilename() + : changeExtension(filename.absFilename(), + formats.extension(loader_format)); + lv->view()->insertPlaintextFile(filename2, as_paragraphs); + lv->dispatch(FuncRequest(LFUN_MARK_OFF)); + } + + // we are done + lv->message(_("imported.")); + return true; +} + + + // This function runs "configure" and then rereads lyx.defaults to // reconfigure the automatic settings. void reconfigure(LyXView & lv, string const & option) @@ -2295,7 +2365,7 @@ void LyXFunc::doImport(string const & argument) } ErrorList errorList; - Importer::Import(lyx_view_, fullname, format, errorList); + import(lyx_view_, fullname, format, errorList); // FIXME (Abdel 12/08/06): Is there a need to display the error list here? } diff --git a/src/Makefile.am b/src/Makefile.am index 318fd20ce8..ec58f7268a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -156,8 +156,6 @@ liblyxcore_la_SOURCES = \ gettext.h \ Graph.cpp \ Graph.h \ - Importer.cpp \ - Importer.h \ InsetIterator.cpp \ InsetIterator.h \ InsetList.cpp \ diff --git a/src/MenuBackend.cpp b/src/MenuBackend.cpp index b35a78375d..2dd4efb731 100644 --- a/src/MenuBackend.cpp +++ b/src/MenuBackend.cpp @@ -21,13 +21,13 @@ #include "Buffer.h" #include "BufferList.h" #include "BufferParams.h" +#include "Converter.h" #include "CutAndPaste.h" #include "debug.h" #include "Floating.h" #include "FloatList.h" #include "Format.h" #include "gettext.h" -#include "Importer.h" #include "KeyMap.h" #include "Session.h" #include "LyXAction.h" @@ -544,7 +544,7 @@ void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf) switch (kind) { case MenuItem::ImportFormats: - formats = Importer::GetImportableFormats(); + formats = theConverters().importableFormats(); action = LFUN_BUFFER_IMPORT; break; case MenuItem::ViewFormats: