3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "BufferParams.h"
17 #include "ServerSocket.h"
19 #include "frontends/alert.h" //to be removed?
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/mutex.h"
26 #include "support/os.h"
27 #include "support/PathChanger.h"
28 #include "support/Systemcall.h"
29 #include "support/textutils.h"
30 #include "support/Translator.h"
36 // FIXME: Q_OS_MAC is not available, it's in Qt
37 #ifdef USE_MACOSX_PACKAGING
38 #include "support/linkback/LinkBackProxy.h"
46 using namespace lyx::support;
50 namespace Alert = frontend::Alert;
51 namespace os = support::os;
55 string const token_from_format("$$i");
56 string const token_path_format("$$p");
57 string const token_socket_format("$$a");
60 class FormatNamesEqual : public unary_function<Format, bool> {
62 FormatNamesEqual(string const & name)
65 bool operator()(Format const & f) const
67 return f.name() == name_;
74 class FormatExtensionsEqual : public unary_function<Format, bool> {
76 FormatExtensionsEqual(string const & extension)
77 : extension_(extension)
79 bool operator()(Format const & f) const
81 return f.hasExtension(extension_);
88 class FormatMimeEqual : public unary_function<Format, bool> {
90 FormatMimeEqual(string const & mime)
93 bool operator()(Format const & f) const
95 // The test for empty mime strings is needed since we allow
96 // formats with empty mime types.
97 return f.mime() == mime_ && !mime_.empty();
106 bool Format::formatSorter(Format const * lhs, Format const * rhs)
108 return compare_locale(_(lhs->prettyname()), _(rhs->prettyname())) < 0;
111 bool operator<(Format const & a, Format const & b)
113 return compare_locale(_(a.prettyname()), _(b.prettyname())) < 0;
117 Format::Format(string const & n, string const & e, string const & p,
118 string const & s, string const & v, string const & ed,
119 string const & m, int flags)
120 : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
121 editor_(ed), mime_(m), flags_(flags)
123 extension_list_ = getVectorFromString(e, ",");
124 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
128 bool Format::dummy() const
130 return extension().empty();
134 string const Format::extensions() const
136 return getStringFromVector(extension_list_, ", ");
140 bool Format::hasExtension(string const & e) const
142 return (find(extension_list_.begin(), extension_list_.end(), e)
143 != extension_list_.end());
147 bool Format::isChildFormat() const
151 return isDigitASCII(name_[name_.length() - 1]);
155 string const Format::parentFormat() const
157 return name_.substr(0, name_.length() - 1);
161 void Format::setExtensions(string const & e)
163 extension_list_ = getVectorFromString(e, ",");
167 // This method should return a reference, and throw an exception
168 // if the format named name cannot be found (Lgb)
169 Format const * Formats::getFormat(string const & name) const
171 FormatList::const_iterator cit =
172 find_if(formatlist.begin(), formatlist.end(),
173 FormatNamesEqual(name));
174 if (cit != formatlist.end())
183 /** Guess the file format name (as in Format::name()) from contents.
184 * Normally you don't want to use this directly, but rather
185 * Formats::getFormatFromFile().
187 string guessFormatFromContents(FileName const & fn)
189 // the different filetypes and what they contain in one of the first lines
190 // (dots are any characters). (Herbert 20020131)
193 // EPS %!PS-Adobe-3.0 EPSF...
197 // JPG \377\330... (0xFFD8)
200 // PBM P1... or P4 (B/W)
201 // PGM P2... or P5 (Grayscale)
202 // PPM P3... or P6 (color)
203 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
204 // SGI \001\332... (decimal 474)
206 // TIFF II... or MM...
208 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
209 // ...static char *...
210 // XWD \000\000\000\151 (0x00006900) decimal 105
212 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
213 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
214 // Z \037\235 UNIX compress
217 if (fn.empty() || !fn.isReadableFile())
220 ifstream ifs(fn.toFilesystemEncoding().c_str());
222 // Couldn't open file...
226 static string const gzipStamp = "\037\213";
229 static string const zipStamp = "PK";
231 // ZIP containers (koffice, openoffice.org etc).
232 static string const nonzipStamp = "\008\0\0\0mimetypeapplication/";
235 static string const compressStamp = "\037\235";
237 // DOS binary EPS according to Adobe TN-5002
238 static string const binEPSStamp = "\xC5\xD0\xD3\xC6";
241 // Maximum strings to read
242 int const max_count = 50;
247 bool firstLine = true;
248 bool backslash = false;
249 bool maybelatex = false;
251 while ((count++ < max_count) && format.empty() && !maybelatex) {
256 string const stamp = str.substr(0, 2);
257 if (firstLine && str.size() >= 2) {
258 // at first we check for a zipped file, because this
259 // information is saved in the first bytes of the file!
260 // also some graphic formats which save the information
261 // in the first line, too.
262 if (prefixIs(str, gzipStamp)) {
265 } else if (stamp == zipStamp &&
266 !contains(str, nonzipStamp)) {
269 } else if (stamp == compressStamp) {
273 } else if (stamp == "BM") {
276 } else if (stamp == "\377\330") {
279 } else if (stamp == "\001\332") {
281 } else if (prefixIs(str, binEPSStamp)) {
285 // Don't need to use str.at(0), str.at(1) because
286 // we already know that str.size() >= 2
287 } else if (str[0] == 'P') {
303 } else if ((stamp == "II") || (stamp == "MM")) {
306 } else if (prefixIs(str,"%TGIF")) {
309 } else if (prefixIs(str,"#FIG")) {
312 } else if (prefixIs(str,"GIF")) {
315 } else if (str.size() > 3) {
316 int const c = ((str[0] << 24) & (str[1] << 16) &
317 (str[2] << 8) & str[3]);
328 else if (contains(str,"EPSF"))
329 // dummy, if we have wrong file description like
330 // %!PS-Adobe-2.0EPSF"
333 else if (contains(str, "Grace"))
336 else if (contains(str, "%PDF"))
337 // autodetect pdf format for graphics inclusion
340 else if (contains(str, "PNG"))
343 else if (contains(str, "%!PS-Adobe")) {
346 if (contains(str,"EPSF"))
352 else if (contains(str, "_bits[]"))
355 else if (contains(str, "XPM") || contains(str, "static char *"))
358 else if (contains(str, "BITPIX"))
361 else if (contains(str, "\\documentclass") ||
362 contains(str, "\\chapter") ||
363 contains(str, "\\section") ||
364 contains(str, "\\begin") ||
365 contains(str, "\\end") ||
366 contains(str, "$$") ||
367 contains(str, "\\[") ||
368 contains(str, "\\]"))
371 if (contains(str, '\\'))
373 dollars += count_char(str, '$');
374 if (backslash && dollars > 1)
380 if (format.empty() && maybelatex && !isBinaryFile(fn))
383 if (format.empty()) {
385 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
386 "\tFile type not recognised before EOF!");
388 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
392 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
393 << "\tCouldn't find a known format!");
400 string Formats::getFormatFromFile(FileName const & filename) const
402 if (filename.empty())
408 if (filename.exists()) {
409 magic_t magic_cookie = magic_open(MAGIC_MIME);
411 if (magic_load(magic_cookie, NULL) != 0) {
412 LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
413 << "\tCouldn't load magic database - "
414 << magic_error(magic_cookie));
416 char const * result = magic_file(magic_cookie,
417 filename.toFilesystemEncoding().c_str());
420 mime = token(result, ';', 0);
422 LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
423 << "\tCouldn't query magic database - "
424 << magic_error(magic_cookie));
426 // our own detection is better for binary files (can be anything)
427 // and different plain text formats
428 if (!mime.empty() && mime != "application/octet-stream" &&
429 mime != "text/plain") {
430 Formats::const_iterator cit =
431 find_if(formatlist.begin(), formatlist.end(),
432 FormatMimeEqual(mime));
433 if (cit != formats.end()) {
434 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
435 << mime << " -> " << cit->name());
436 // See special eps/ps handling below
437 if (mime == "application/postscript")
438 psformat = cit->name();
440 format = cit->name();
444 magic_close(magic_cookie);
445 // libmagic recognizes as latex also some formats of ours
446 // such as pstex and pdftex. Therefore we have to perform
447 // additional checks in this case (bug 9244).
448 if (!format.empty() && format != "latex")
454 string const ext = getExtension(filename.absFileName());
455 if (format.empty()) {
456 // libmagic does not distinguish eps and ps.
457 // Therefore we need to use our own detection here, but only if it
458 // recognizes either ps or eps. Otherwise the libmagic guess will
459 // be better (bug 9146).
460 format = guessFormatFromContents(filename);
461 if (!psformat.empty()) {
462 if (isPostScriptFileFormat(format))
468 if (isZippedFileFormat(format) && !ext.empty()) {
469 string const & fmt_name = formats.getFormatFromExtension(ext);
470 if (!fmt_name.empty()) {
471 Format const * p_format = formats.getFormat(fmt_name);
472 if (p_format && p_format->zippedNative())
473 return p_format->name();
476 // Don't simply return latex (bug 9244).
477 if (!format.empty() && format != "latex")
481 // Both libmagic and our guessing from contents may return as latex
482 // also lyx files and our pstex and pdftex formats. In this case we
483 // give precedence to the format determined by the extension.
484 if (format == "latex") {
485 format = getFormatFromExtension(ext);
486 return format.empty() ? "latex" : format;
489 // try to find a format from the file extension.
490 return getFormatFromExtension(ext);
494 string Formats::getFormatFromExtension(string const & ext) const
497 // this is ambigous if two formats have the same extension,
498 // but better than nothing
499 Formats::const_iterator cit =
500 find_if(formatlist.begin(), formatlist.end(),
501 FormatExtensionsEqual(ext));
502 if (cit != formats.end()) {
503 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
504 << ext << " -> " << cit->name());
512 /// Used to store last timestamp of file and whether it is (was) zipped
515 std::time_t timestamp;
516 ZippedInfo(bool zipped, std::time_t timestamp)
517 : zipped(zipped), timestamp(timestamp) { }
521 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
522 static std::map<std::string, ZippedInfo> zipped_;
523 static Mutex zipped_mutex;
526 bool Formats::isZippedFile(support::FileName const & filename) const {
527 string const & fname = filename.absFileName();
528 time_t timestamp = filename.lastModified();
529 Mutex::Locker lock(&zipped_mutex);
530 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
531 if (it != zipped_.end() && it->second.timestamp == timestamp)
532 return it->second.zipped;
533 string const & format = getFormatFromFile(filename);
534 bool zipped = (format == "gzip" || format == "zip");
535 zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
540 bool Formats::isZippedFileFormat(string const & format)
542 return contains("gzip zip compress", format) && !format.empty();
546 bool Formats::isPostScriptFileFormat(string const & format)
548 return format == "ps" || format == "eps";
551 static string fixCommand(string const & cmd, string const & ext,
552 os::auto_open_mode mode)
554 // configure.py says we do not want a viewer/editor
558 // Does the OS manage this format?
559 if (os::canAutoOpenFile(ext, mode))
562 // if configure.py found nothing, clear the command
563 if (token(cmd, ' ', 0) == "auto")
566 // use the command found by configure.py
571 void Formats::setAutoOpen()
573 FormatList::iterator fit = formatlist.begin();
574 FormatList::iterator const fend = formatlist.end();
575 for ( ; fit != fend ; ++fit) {
576 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
577 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
582 int Formats::getNumber(string const & name) const
584 FormatList::const_iterator cit =
585 find_if(formatlist.begin(), formatlist.end(),
586 FormatNamesEqual(name));
587 if (cit != formatlist.end())
588 return distance(formatlist.begin(), cit);
594 void Formats::add(string const & name)
596 if (!getFormat(name))
597 add(name, name, name, string(), string(), string(),
598 string(), Format::document);
602 void Formats::add(string const & name, string const & extensions,
603 string const & prettyname, string const & shortcut,
604 string const & viewer, string const & editor,
605 string const & mime, int flags)
607 FormatList::iterator it =
608 find_if(formatlist.begin(), formatlist.end(),
609 FormatNamesEqual(name));
610 if (it == formatlist.end())
611 formatlist.push_back(Format(name, extensions, prettyname,
612 shortcut, viewer, editor, mime, flags));
614 *it = Format(name, extensions, prettyname, shortcut, viewer,
615 editor, mime, flags);
619 void Formats::erase(string const & name)
621 FormatList::iterator it =
622 find_if(formatlist.begin(), formatlist.end(),
623 FormatNamesEqual(name));
624 if (it != formatlist.end())
625 formatlist.erase(it);
631 std::sort(formatlist.begin(), formatlist.end());
635 void Formats::setViewer(string const & name, string const & command)
638 FormatList::iterator it =
639 find_if(formatlist.begin(), formatlist.end(),
640 FormatNamesEqual(name));
641 if (it != formatlist.end())
642 it->setViewer(command);
646 void Formats::setEditor(string const & name, string const & command)
649 FormatList::iterator it =
650 find_if(formatlist.begin(), formatlist.end(),
651 FormatNamesEqual(name));
652 if (it != formatlist.end())
653 it->setEditor(command);
657 bool Formats::view(Buffer const & buffer, FileName const & filename,
658 string const & format_name) const
660 if (filename.empty() || !filename.exists()) {
661 Alert::error(_("Cannot view file"),
662 bformat(_("File does not exist: %1$s"),
663 from_utf8(filename.absFileName())));
667 Format const * format = getFormat(format_name);
668 if (format && format->viewer().empty() &&
669 format->isChildFormat())
670 format = getFormat(format->parentFormat());
671 if (!format || format->viewer().empty()) {
672 // FIXME: I believe this is the wrong place to show alerts, it should be done
673 // by the caller (this should be "utility" code)
674 Alert::error(_("Cannot view file"),
675 bformat(_("No information for viewing %1$s"),
676 prettyName(format_name)));
680 if (format->viewer() == "auto") {
681 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
684 Alert::error(_("Cannot view file"),
685 bformat(_("Auto-view file %1$s failed"),
686 from_utf8(filename.absFileName())));
691 string command = format->viewer();
693 if (format_name == "dvi" &&
694 !lyxrc.view_dvi_paper_option.empty()) {
695 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
696 if (!paper_size.empty()) {
697 command += ' ' + lyxrc.view_dvi_paper_option;
698 command += ' ' + paper_size;
699 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
700 buffer.params().papersize != PAPER_CUSTOM)
705 if (!contains(command, token_from_format))
706 command += ' ' + token_from_format;
708 command = subst(command, token_from_format,
709 quoteName(onlyFileName(filename.toFilesystemEncoding()), quote_shell_filename));
710 command = subst(command, token_path_format,
711 quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
712 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
713 LYXERR(Debug::FILES, "Executing command: " << command);
714 // FIXME UNICODE utf8 can be wrong for files
715 buffer.message(_("Executing command: ") + from_utf8(command));
717 PathChanger p(filename.onlyPath());
719 one.startscript(Systemcall::DontWait, command,
720 buffer.filePath(), buffer.layoutPos());
722 // we can't report any sort of error, since we aren't waiting
727 bool Formats::edit(Buffer const & buffer, FileName const & filename,
728 string const & format_name) const
730 if (filename.empty() || !filename.exists()) {
731 Alert::error(_("Cannot edit file"),
732 bformat(_("File does not exist: %1$s"),
733 from_utf8(filename.absFileName())));
737 // LinkBack files look like PDF, but have the .linkback extension
738 string const ext = getExtension(filename.absFileName());
739 if (format_name == "pdf6" && ext == "linkback") {
740 #ifdef USE_MACOSX_PACKAGING
741 return editLinkBackFile(filename.absFileName().c_str());
743 Alert::error(_("Cannot edit file"),
744 _("LinkBack files can only be edited on Apple Mac OSX."));
746 #endif // USE_MACOSX_PACKAGING
749 Format const * format = getFormat(format_name);
750 if (format && format->editor().empty() &&
751 format->isChildFormat())
752 format = getFormat(format->parentFormat());
753 if (!format || format->editor().empty()) {
754 // FIXME: I believe this is the wrong place to show alerts, it should
755 // be done by the caller (this should be "utility" code)
756 Alert::error(_("Cannot edit file"),
757 bformat(_("No information for editing %1$s"),
758 prettyName(format_name)));
763 if (format->editor() == "auto") {
764 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
767 Alert::error(_("Cannot edit file"),
768 bformat(_("Auto-edit file %1$s failed"),
769 from_utf8(filename.absFileName())));
774 string command = format->editor();
776 if (!contains(command, token_from_format))
777 command += ' ' + token_from_format;
779 command = subst(command, token_from_format,
780 quoteName(filename.toFilesystemEncoding(), quote_shell_filename));
781 command = subst(command, token_path_format,
782 quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
783 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
784 LYXERR(Debug::FILES, "Executing command: " << command);
785 // FIXME UNICODE utf8 can be wrong for files
786 buffer.message(_("Executing command: ") + from_utf8(command));
789 one.startscript(Systemcall::DontWait, command,
790 buffer.filePath(), buffer.layoutPos());
792 // we can't report any sort of error, since we aren't waiting
797 docstring const Formats::prettyName(string const & name) const
799 Format const * format = getFormat(name);
801 return from_utf8(format->prettyname());
803 return from_utf8(name);
807 string const Formats::extension(string const & name) const
809 Format const * format = getFormat(name);
811 return format->extension();
817 string const Formats::extensions(string const & name) const
819 Format const * format = getFormat(name);
821 return format->extensions();
829 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
832 FlavorTranslator initFlavorTranslator()
834 FlavorTranslator f(OutputParams::LATEX, "latex");
835 f.addPair(OutputParams::DVILUATEX, "dviluatex");
836 f.addPair(OutputParams::LUATEX, "luatex");
837 f.addPair(OutputParams::PDFLATEX, "pdflatex");
838 f.addPair(OutputParams::XETEX, "xetex");
839 f.addPair(OutputParams::XML, "docbook-xml");
840 f.addPair(OutputParams::HTML, "xhtml");
841 f.addPair(OutputParams::TEXT, "text");
846 FlavorTranslator const & flavorTranslator()
848 static FlavorTranslator const translator = initFlavorTranslator();
855 std::string flavor2format(OutputParams::FLAVOR flavor)
857 return flavorTranslator().find(flavor);
861 /* Not currently needed, but I'll leave the code in case it is.
862 OutputParams::FLAVOR format2flavor(std::string fmt)
864 return flavorTranslator().find(fmt);
869 Formats system_formats;