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/os.h"
26 #include "support/Path.h"
27 #include "support/Systemcall.h"
28 #include "support/textutils.h"
29 #include "support/Translator.h"
35 // FIXME: Q_WS_MACX is not available, it's in Qt
36 #ifdef USE_MACOSX_PACKAGING
37 #include "support/linkback/LinkBackProxy.h"
45 using namespace lyx::support;
49 namespace Alert = frontend::Alert;
50 namespace os = support::os;
54 string const token_from_format("$$i");
55 string const token_path_format("$$p");
56 string const token_socket_format("$$a");
59 class FormatNamesEqual : public unary_function<Format, bool> {
61 FormatNamesEqual(string const & name)
63 bool operator()(Format const & f) const
65 return f.name() == name_;
72 class FormatExtensionsEqual : public unary_function<Format, bool> {
74 FormatExtensionsEqual(string const & extension)
75 : extension_(extension) {}
76 bool operator()(Format const & f) const
78 return f.hasExtension(extension_);
85 class FormatMimeEqual : public unary_function<Format, bool> {
87 FormatMimeEqual(string const & mime)
89 bool operator()(Format const & f) const
91 // The test for empty mime strings is needed since we allow
92 // formats with empty mime types.
93 return f.mime() == mime_ && !mime_.empty();
100 class FormatPrettyNameEqual : public unary_function<Format, bool> {
102 FormatPrettyNameEqual(string const & prettyname)
103 : prettyname_(prettyname) {}
104 bool operator()(Format const & f) const
106 return f.prettyname() == prettyname_;
115 bool operator<(Format const & a, Format const & b)
117 return _(a.prettyname()) < _(b.prettyname());
121 Format::Format(string const & n, string const & e, string const & p,
122 string const & s, string const & v, string const & ed,
123 string const & m, int flags)
124 : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
125 editor_(ed), mime_(m), flags_(flags)
127 extension_list_ = getVectorFromString(e, ",");
128 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
132 bool Format::dummy() const
134 return extension().empty();
138 string const Format::extensions() const
140 return getStringFromVector(extension_list_, ", ");
144 bool Format::hasExtension(string const & e) const
146 return (find(extension_list_.begin(), extension_list_.end(), e)
147 != extension_list_.end());
151 bool Format::isChildFormat() const
155 return isDigitASCII(name_[name_.length() - 1]);
159 string const Format::parentFormat() const
161 return name_.substr(0, name_.length() - 1);
165 void Format::setExtensions(string const & e)
167 extension_list_ = getVectorFromString(e, ",");
171 // This method should return a reference, and throw an exception
172 // if the format named name cannot be found (Lgb)
173 Format const * Formats::getFormat(string const & name) const
175 FormatList::const_iterator cit =
176 find_if(formatlist.begin(), formatlist.end(),
177 FormatNamesEqual(name));
178 if (cit != formatlist.end())
187 /** Guess the file format name (as in Format::name()) from contents.
188 * Normally you don't want to use this directly, but rather
189 * Formats::getFormatFromFile().
191 string guessFormatFromContents(FileName const & fn)
193 // the different filetypes and what they contain in one of the first lines
194 // (dots are any characters). (Herbert 20020131)
197 // EPS %!PS-Adobe-3.0 EPSF...
204 // PBM P1... or P4 (B/W)
205 // PGM P2... or P5 (Grayscale)
206 // PPM P3... or P6 (color)
207 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
208 // SGI \001\332... (decimal 474)
210 // TIFF II... or MM...
212 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
213 // ...static char *...
214 // XWD \000\000\000\151 (0x00006900) decimal 105
216 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
217 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
218 // Z \037\235 UNIX compress
221 if (fn.empty() || !fn.isReadableFile())
224 ifstream ifs(fn.toFilesystemEncoding().c_str());
226 // Couldn't open file...
230 static string const gzipStamp = "\037\213";
233 static string const zipStamp = "PK";
235 // ZIP containers (koffice, openoffice.org etc).
236 static string const nonzipStamp = "\008\0\0\0mimetypeapplication/";
239 static string const compressStamp = "\037\235";
241 // Maximum strings to read
242 int const max_count = 50;
247 bool firstLine = true;
248 while ((count++ < max_count) && format.empty()) {
250 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
251 << "\tFile type not recognised before EOF!");
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 == "\001\332") {
280 // Don't need to use str.at(0), str.at(1) because
281 // we already know that str.size() >= 2
282 } else if (str[0] == 'P') {
298 } else if ((stamp == "II") || (stamp == "MM")) {
301 } else if (prefixIs(str,"%TGIF")) {
304 } else if (prefixIs(str,"#FIG")) {
307 } else if (prefixIs(str,"GIF")) {
310 } else if (str.size() > 3) {
311 int const c = ((str[0] << 24) & (str[1] << 16) &
312 (str[2] << 8) & str[3]);
323 else if (contains(str,"EPSF"))
324 // dummy, if we have wrong file description like
325 // %!PS-Adobe-2.0EPSF"
328 else if (contains(str, "Grace"))
331 else if (contains(str, "JFIF"))
334 else if (contains(str, "%PDF"))
337 else if (contains(str, "PNG"))
340 else if (contains(str, "%!PS-Adobe")) {
343 if (contains(str,"EPSF"))
349 else if (contains(str, "_bits[]"))
352 else if (contains(str, "XPM") || contains(str, "static char *"))
355 else if (contains(str, "BITPIX"))
359 if (!format.empty()) {
360 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
364 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
365 << "\tCouldn't find a known format!");
372 string Formats::getFormatFromFile(FileName const & filename) const
374 if (filename.empty())
378 if (filename.exists()) {
379 magic_t magic_cookie = magic_open(MAGIC_MIME);
382 if (magic_load(magic_cookie, NULL) != 0) {
383 LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
384 << "\tCouldn't load magic database - "
385 << magic_error(magic_cookie));
387 string mime = magic_file(magic_cookie,
388 filename.toFilesystemEncoding().c_str());
389 mime = token(mime, ';', 0);
390 // we need our own ps/eps detection
391 if ((mime != "application/postscript") && (mime != "text/plain")) {
392 Formats::const_iterator cit =
393 find_if(formatlist.begin(), formatlist.end(),
394 FormatMimeEqual(mime));
395 if (cit != formats.end()) {
396 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
397 << mime << " -> " << cit->name());
398 format = cit->name();
402 magic_close(magic_cookie);
409 string const format = guessFormatFromContents(filename);
410 string const ext = getExtension(filename.absFileName());
411 if (isZippedFileFormat(format) && !ext.empty()) {
412 string const & fmt_name = formats.getFormatFromExtension(ext);
413 if (!fmt_name.empty()) {
414 Format const * p_format = formats.getFormat(fmt_name);
415 if (p_format && p_format->zippedNative())
416 return p_format->name();
422 // try to find a format from the file extension.
423 return getFormatFromExtension(ext);
427 string Formats::getFormatFromExtension(string const & ext) const
430 // this is ambigous if two formats have the same extension,
431 // but better than nothing
432 Formats::const_iterator cit =
433 find_if(formatlist.begin(), formatlist.end(),
434 FormatExtensionsEqual(ext));
435 if (cit != formats.end()) {
436 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
437 << ext << " -> " << cit->name());
445 string Formats::getFormatFromPrettyName(string const & prettyname) const
447 if (!prettyname.empty()) {
448 Formats::const_iterator cit =
449 find_if(formatlist.begin(), formatlist.end(),
450 FormatPrettyNameEqual(prettyname));
451 if (cit != formats.end())
458 /// Used to store last timestamp of file and whether it is (was) zipped
461 std::time_t timestamp;
462 ZippedInfo(bool zipped, std::time_t timestamp)
463 : zipped(zipped), timestamp(timestamp) { }
467 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
468 static std::map<std::string, ZippedInfo> zipped_;
471 bool Formats::isZippedFile(support::FileName const & filename) const {
472 string const & fname = filename.absFileName();
473 time_t timestamp = filename.lastModified();
474 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
475 if (it != zipped_.end() && it->second.timestamp == timestamp)
476 return it->second.zipped;
477 string const & format = getFormatFromFile(filename);
478 bool zipped = (format == "gzip" || format == "zip");
479 zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
484 bool Formats::isZippedFileFormat(string const & format)
486 return contains("gzip zip compress", format) && !format.empty();
490 bool Formats::isPostScriptFileFormat(string const & format)
492 return format == "ps" || format == "eps";
495 static string fixCommand(string const & cmd, string const & ext,
496 os::auto_open_mode mode)
498 // configure.py says we do not want a viewer/editor
502 // Does the OS manage this format?
503 if (os::canAutoOpenFile(ext, mode))
506 // if configure.py found nothing, clear the command
507 if (token(cmd, ' ', 0) == "auto")
510 // use the command found by configure.py
515 void Formats::setAutoOpen()
517 FormatList::iterator fit = formatlist.begin();
518 FormatList::iterator const fend = formatlist.end();
519 for ( ; fit != fend ; ++fit) {
520 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
521 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
526 int Formats::getNumber(string const & name) const
528 FormatList::const_iterator cit =
529 find_if(formatlist.begin(), formatlist.end(),
530 FormatNamesEqual(name));
531 if (cit != formatlist.end())
532 return distance(formatlist.begin(), cit);
538 void Formats::add(string const & name)
540 if (!getFormat(name))
541 add(name, name, name, string(), string(), string(),
542 string(), Format::document);
546 void Formats::add(string const & name, string const & extensions,
547 string const & prettyname, string const & shortcut,
548 string const & viewer, string const & editor,
549 string const & mime, int flags)
551 FormatList::iterator it =
552 find_if(formatlist.begin(), formatlist.end(),
553 FormatNamesEqual(name));
554 if (it == formatlist.end())
555 formatlist.push_back(Format(name, extensions, prettyname,
556 shortcut, viewer, editor, mime, flags));
558 *it = Format(name, extensions, prettyname, shortcut, viewer,
559 editor, mime, flags);
563 void Formats::erase(string const & name)
565 FormatList::iterator it =
566 find_if(formatlist.begin(), formatlist.end(),
567 FormatNamesEqual(name));
568 if (it != formatlist.end())
569 formatlist.erase(it);
575 std::sort(formatlist.begin(), formatlist.end());
579 void Formats::setViewer(string const & name, string const & command)
582 FormatList::iterator it =
583 find_if(formatlist.begin(), formatlist.end(),
584 FormatNamesEqual(name));
585 if (it != formatlist.end())
586 it->setViewer(command);
590 void Formats::setEditor(string const & name, string const & command)
593 FormatList::iterator it =
594 find_if(formatlist.begin(), formatlist.end(),
595 FormatNamesEqual(name));
596 if (it != formatlist.end())
597 it->setEditor(command);
601 bool Formats::view(Buffer const & buffer, FileName const & filename,
602 string const & format_name) const
604 if (filename.empty() || !filename.exists()) {
605 Alert::error(_("Cannot view file"),
606 bformat(_("File does not exist: %1$s"),
607 from_utf8(filename.absFileName())));
611 Format const * format = getFormat(format_name);
612 if (format && format->viewer().empty() &&
613 format->isChildFormat())
614 format = getFormat(format->parentFormat());
615 if (!format || format->viewer().empty()) {
616 // FIXME: I believe this is the wrong place to show alerts, it should be done
617 // by the caller (this should be "utility" code)
618 Alert::error(_("Cannot view file"),
619 bformat(_("No information for viewing %1$s"),
620 prettyName(format_name)));
624 if (format->viewer() == "auto") {
625 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
628 Alert::error(_("Cannot view file"),
629 bformat(_("Auto-view file %1$s failed"),
630 from_utf8(filename.absFileName())));
635 string command = libScriptSearch(format->viewer());
637 if (format_name == "dvi" &&
638 !lyxrc.view_dvi_paper_option.empty()) {
639 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
640 if (!paper_size.empty()) {
641 command += ' ' + lyxrc.view_dvi_paper_option;
642 command += ' ' + paper_size;
643 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
644 buffer.params().papersize != PAPER_CUSTOM)
649 if (!contains(command, token_from_format))
650 command += ' ' + token_from_format;
652 command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
653 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
654 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
655 LYXERR(Debug::FILES, "Executing command: " << command);
656 // FIXME UNICODE utf8 can be wrong for files
657 buffer.message(_("Executing command: ") + from_utf8(command));
659 PathChanger p(filename.onlyPath());
661 one.startscript(Systemcall::DontWait, command, buffer.filePath());
663 // we can't report any sort of error, since we aren't waiting
668 bool Formats::edit(Buffer const & buffer, FileName const & filename,
669 string const & format_name) const
671 if (filename.empty() || !filename.exists()) {
672 Alert::error(_("Cannot edit file"),
673 bformat(_("File does not exist: %1$s"),
674 from_utf8(filename.absFileName())));
678 // LinkBack files look like PDF, but have the .linkback extension
679 string const ext = getExtension(filename.absFileName());
680 if (format_name == "pdf" && ext == "linkback") {
681 #ifdef USE_MACOSX_PACKAGING
682 return editLinkBackFile(filename.absFileName().c_str());
684 Alert::error(_("Cannot edit file"),
685 _("LinkBack files can only be edited on Apple Mac OSX."));
687 #endif // USE_MACOSX_PACKAGING
690 Format const * format = getFormat(format_name);
691 if (format && format->editor().empty() &&
692 format->isChildFormat())
693 format = getFormat(format->parentFormat());
694 if (!format || format->editor().empty()) {
695 // FIXME: I believe this is the wrong place to show alerts, it should
696 // be done by the caller (this should be "utility" code)
697 Alert::error(_("Cannot edit file"),
698 bformat(_("No information for editing %1$s"),
699 prettyName(format_name)));
704 if (format->editor() == "auto") {
705 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
708 Alert::error(_("Cannot edit file"),
709 bformat(_("Auto-edit file %1$s failed"),
710 from_utf8(filename.absFileName())));
715 string command = format->editor();
717 if (!contains(command, token_from_format))
718 command += ' ' + token_from_format;
720 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
721 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
722 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
723 LYXERR(Debug::FILES, "Executing command: " << command);
724 // FIXME UNICODE utf8 can be wrong for files
725 buffer.message(_("Executing command: ") + from_utf8(command));
728 one.startscript(Systemcall::DontWait, command, buffer.filePath());
730 // we can't report any sort of error, since we aren't waiting
735 docstring const Formats::prettyName(string const & name) const
737 Format const * format = getFormat(name);
739 return from_utf8(format->prettyname());
741 return from_utf8(name);
745 string const Formats::extension(string const & name) const
747 Format const * format = getFormat(name);
749 return format->extension();
755 string const Formats::extensions(string const & name) const
757 Format const * format = getFormat(name);
759 return format->extensions();
766 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
768 FlavorTranslator initFlavorTranslator()
770 FlavorTranslator f(OutputParams::LATEX, "latex");
771 f.addPair(OutputParams::DVILUATEX, "dviluatex");
772 f.addPair(OutputParams::LUATEX, "luatex");
773 f.addPair(OutputParams::PDFLATEX, "pdflatex");
774 f.addPair(OutputParams::XETEX, "xetex");
775 f.addPair(OutputParams::XML, "docbook-xml");
776 f.addPair(OutputParams::HTML, "xhtml");
777 f.addPair(OutputParams::TEXT, "text");
782 FlavorTranslator const & flavorTranslator()
784 static FlavorTranslator translator = initFlavorTranslator();
790 std::string flavor2format(OutputParams::FLAVOR flavor)
792 return flavorTranslator().find(flavor);
796 /* Not currently needed, but I'll leave the code in case it is.
797 OutputParams::FLAVOR format2flavor(std::string fmt)
799 return flavorTranslator().find(fmt);
804 Formats system_formats;