X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FFormat.cpp;h=2ef745e50d12a9d38f03ee0c52f35c50414ff85f;hb=772325ff70c341a2cff1201efb29f75871cc371d;hp=cc85b8500008165cb586f6c2ebccb2fb80fefaf3;hpb=1717ef203cf08fd393e2b905a9ed7f9465c8f47d;p=lyx.git diff --git a/src/Format.cpp b/src/Format.cpp index cc85b85000..2ef745e50d 100644 --- a/src/Format.cpp +++ b/src/Format.cpp @@ -23,11 +23,14 @@ #include "support/gettext.h" #include "support/lstrings.h" #include "support/os.h" +#include "support/Path.h" #include "support/Systemcall.h" #include "support/textutils.h" #include "support/Translator.h" #include +#include +#include // FIXME: Q_WS_MACX is not available, it's in Qt #ifdef USE_MACOSX_PACKAGING @@ -68,7 +71,7 @@ public: : extension_(extension) {} bool operator()(Format const & f) const { - return f.extension() == extension_; + return f.hasExtension(extension_); } private: string extension_; @@ -76,22 +79,22 @@ private: } //namespace anon + bool operator<(Format const & a, Format const & b) { - // use the compare_ascii_no_case instead of compare_no_case, - // because in turkish, 'i' is not the lowercase version of 'I', - // and thus turkish locale breaks parsing of tags. - - return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0; + return _(a.prettyname()) < _(b.prettyname()); } Format::Format(string const & n, string const & e, string const & p, string const & s, string const & v, string const & ed, int flags) - : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v), + : name_(n), prettyname_(p), shortcut_(s), viewer_(v), editor_(ed), flags_(flags) -{} +{ + extension_list_ = getVectorFromString(e, ","); + LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags); +} bool Format::dummy() const @@ -100,6 +103,19 @@ bool Format::dummy() const } +string const Format::extensions() const +{ + return getStringFromVector(extension_list_, ", "); +} + + +bool Format::hasExtension(string const & e) const +{ + return (find(extension_list_.begin(), extension_list_.end(), e) + != extension_list_.end()); +} + + bool Format::isChildFormat() const { if (name_.empty()) @@ -114,6 +130,12 @@ string const Format::parentFormat() const } +void Format::setExtensions(string const & e) +{ + extension_list_ = getVectorFromString(e, ","); +} + + // This method should return a reference, and throw an exception // if the format named name cannot be found (Lgb) Format const * Formats::getFormat(string const & name) const @@ -134,11 +156,26 @@ string Formats::getFormatFromFile(FileName const & filename) const return string(); string const format = filename.guessFormatFromContents(); + string const ext = getExtension(filename.absFileName()); + if ((format == "gzip" || format == "zip" || format == "compress") + && !ext.empty()) { + string const & fmt_name = formats.getFormatFromExtension(ext); + if (!fmt_name.empty()) { + Format const * p_format = formats.getFormat(fmt_name); + if (p_format && p_format->zippedNative()) + return p_format->name(); + } + } if (!format.empty()) return format; // try to find a format from the file extension. - string const ext = getExtension(filename.absFileName()); + return getFormatFromExtension(ext); +} + + +string Formats::getFormatFromExtension(string const & ext) const +{ if (!ext.empty()) { // this is ambigous if two formats have the same extension, // but better than nothing @@ -155,6 +192,32 @@ string Formats::getFormatFromFile(FileName const & filename) const } +/// Used to store last timestamp of file and whether it is (was) zipped +struct ZippedInfo { + bool zipped; + std::time_t timestamp; + ZippedInfo(bool zipped, std::time_t timestamp) + : zipped(zipped), timestamp(timestamp) { } +}; + + +/// Mapping absolute pathnames of files to their ZippedInfo metadata. +static std::map zipped_; + + +bool Formats::isZippedFile(support::FileName const & filename) const { + string const & fname = filename.absFileName(); + time_t timestamp = filename.lastModified(); + map::iterator it = zipped_.find(fname); + if (it != zipped_.end() && it->second.timestamp == timestamp) + return it->second.zipped; + string const & format = getFormatFromFile(filename); + bool zipped = (format == "gzip" || format == "zip"); + zipped_.insert(pair(fname, ZippedInfo(zipped, timestamp))); + return zipped; +} + + static string fixCommand(string const & cmd, string const & ext, os::auto_open_mode mode) { @@ -206,7 +269,7 @@ void Formats::add(string const & name) } -void Formats::add(string const & name, string const & extension, +void Formats::add(string const & name, string const & extensions, string const & prettyname, string const & shortcut, string const & viewer, string const & editor, int flags) @@ -215,10 +278,10 @@ void Formats::add(string const & name, string const & extension, find_if(formatlist.begin(), formatlist.end(), FormatNamesEqual(name)); if (it == formatlist.end()) - formatlist.push_back(Format(name, extension, prettyname, + formatlist.push_back(Format(name, extensions, prettyname, shortcut, viewer, editor, flags)); else - *it = Format(name, extension, prettyname, shortcut, viewer, + *it = Format(name, extensions, prettyname, shortcut, viewer, editor, flags); } @@ -312,13 +375,14 @@ bool Formats::view(Buffer const & buffer, FileName const & filename, if (!contains(command, token_from_format)) command += ' ' + token_from_format; - command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding())); + command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding()))); command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding()))); command = subst(command, token_socket_format, quoteName(theServerSocket().address())); LYXERR(Debug::FILES, "Executing command: " << command); // FIXME UNICODE utf8 can be wrong for files buffer.message(_("Executing command: ") + from_utf8(command)); + PathChanger p(filename.onlyPath()); Systemcall one; one.startscript(Systemcall::DontWait, command, buffer.filePath()); @@ -414,12 +478,23 @@ string const Formats::extension(string const & name) const } +string const Formats::extensions(string const & name) const +{ + Format const * format = getFormat(name); + if (format) + return format->extensions(); + else + return name; +} + + namespace { typedef Translator FlavorTranslator; FlavorTranslator initFlavorTranslator() { FlavorTranslator f(OutputParams::LATEX, "latex"); + f.addPair(OutputParams::DVILUATEX, "dviluatex"); f.addPair(OutputParams::LUATEX, "luatex"); f.addPair(OutputParams::PDFLATEX, "pdflatex"); f.addPair(OutputParams::XETEX, "xetex");