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"
41 using namespace lyx::support;
45 namespace Alert = frontend::Alert;
46 namespace os = support::os;
50 string const token_from_format("$$i");
51 string const token_path_format("$$p");
52 string const token_socket_format("$$a");
55 class FormatNamesEqual : public unary_function<Format, bool> {
57 FormatNamesEqual(string const & name)
59 bool operator()(Format const & f) const
61 return f.name() == name_;
68 class FormatExtensionsEqual : public unary_function<Format, bool> {
70 FormatExtensionsEqual(string const & extension)
71 : extension_(extension) {}
72 bool operator()(Format const & f) const
74 return f.hasExtension(extension_);
83 bool operator<(Format const & a, Format const & b)
85 // use the compare_ascii_no_case instead of compare_no_case,
86 // because in turkish, 'i' is not the lowercase version of 'I',
87 // and thus turkish locale breaks parsing of tags.
89 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
93 Format::Format(string const & n, string const & e, string const & p,
94 string const & s, string const & v, string const & ed,
96 : name_(n), extensions_(e), prettyname_(p), shortcut_(s), viewer_(v),
97 editor_(ed), flags_(flags)
99 extension_list_ = getVectorFromString(e, ",");
100 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
104 bool Format::dummy() const
106 return extension().empty();
110 bool Format::hasExtension(string const & e) const
112 return (find(extension_list_.begin(), extension_list_.end(), e)
113 != extension_list_.end());
117 bool Format::isChildFormat() const
121 return isDigitASCII(name_[name_.length() - 1]);
125 string const Format::parentFormat() const
127 return name_.substr(0, name_.length() - 1);
131 void Format::setExtensions(string const & e)
134 extension_list_ = getVectorFromString(e, ",");
138 // This method should return a reference, and throw an exception
139 // if the format named name cannot be found (Lgb)
140 Format const * Formats::getFormat(string const & name) const
142 FormatList::const_iterator cit =
143 find_if(formatlist.begin(), formatlist.end(),
144 FormatNamesEqual(name));
145 if (cit != formatlist.end())
152 string Formats::getFormatFromFile(FileName const & filename) const
154 if (filename.empty())
157 string const format = filename.guessFormatFromContents();
158 string const ext = getExtension(filename.absFileName());
159 if ((format == "gzip" || format == "zip" || format == "compress")
161 string const & fmt_name = formats.getFormatFromExtension(ext);
162 if (!fmt_name.empty()) {
163 Format const * p_format = formats.getFormat(fmt_name);
164 if (p_format && p_format->zippedNative())
165 return p_format->name();
171 // try to find a format from the file extension.
172 return getFormatFromExtension(ext);
176 string Formats::getFormatFromExtension(string const & ext) const
179 // this is ambigous if two formats have the same extension,
180 // but better than nothing
181 Formats::const_iterator cit =
182 find_if(formatlist.begin(), formatlist.end(),
183 FormatExtensionsEqual(ext));
184 if (cit != formats.end()) {
185 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
186 << ext << " -> " << cit->name());
194 /// Used to store last timestamp of file and whether it is (was) zipped
197 std::time_t timestamp;
198 ZippedInfo(bool zipped, std::time_t timestamp)
199 : zipped(zipped), timestamp(timestamp) { }
203 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
204 static std::map<std::string, ZippedInfo> zipped_;
207 bool Formats::isZippedFile(support::FileName const & filename) const {
208 string const & fname = filename.absFileName();
209 time_t timestamp = filename.lastModified();
210 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
211 if (it != zipped_.end() && it->second.timestamp == timestamp)
212 return it->second.zipped;
213 string const & format = getFormatFromFile(filename);
214 bool zipped = (format == "gzip" || format == "zip");
215 zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
220 static string fixCommand(string const & cmd, string const & ext,
221 os::auto_open_mode mode)
223 // configure.py says we do not want a viewer/editor
227 // Does the OS manage this format?
228 if (os::canAutoOpenFile(ext, mode))
231 // if configure.py found nothing, clear the command
232 if (token(cmd, ' ', 0) == "auto")
235 // use the command found by configure.py
240 void Formats::setAutoOpen()
242 FormatList::iterator fit = formatlist.begin();
243 FormatList::iterator const fend = formatlist.end();
244 for ( ; fit != fend ; ++fit) {
245 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
246 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
251 int Formats::getNumber(string const & name) const
253 FormatList::const_iterator cit =
254 find_if(formatlist.begin(), formatlist.end(),
255 FormatNamesEqual(name));
256 if (cit != formatlist.end())
257 return distance(formatlist.begin(), cit);
263 void Formats::add(string const & name)
265 if (!getFormat(name))
266 add(name, name, name, string(), string(), string(),
271 void Formats::add(string const & name, string const & extensions,
272 string const & prettyname, string const & shortcut,
273 string const & viewer, string const & editor,
276 FormatList::iterator it =
277 find_if(formatlist.begin(), formatlist.end(),
278 FormatNamesEqual(name));
279 if (it == formatlist.end())
280 formatlist.push_back(Format(name, extensions, prettyname,
281 shortcut, viewer, editor, flags));
283 *it = Format(name, extensions, prettyname, shortcut, viewer,
288 void Formats::erase(string const & name)
290 FormatList::iterator it =
291 find_if(formatlist.begin(), formatlist.end(),
292 FormatNamesEqual(name));
293 if (it != formatlist.end())
294 formatlist.erase(it);
300 std::sort(formatlist.begin(), formatlist.end());
304 void Formats::setViewer(string const & name, string const & command)
307 FormatList::iterator it =
308 find_if(formatlist.begin(), formatlist.end(),
309 FormatNamesEqual(name));
310 if (it != formatlist.end())
311 it->setViewer(command);
315 void Formats::setEditor(string const & name, string const & command)
318 FormatList::iterator it =
319 find_if(formatlist.begin(), formatlist.end(),
320 FormatNamesEqual(name));
321 if (it != formatlist.end())
322 it->setEditor(command);
326 bool Formats::view(Buffer const & buffer, FileName const & filename,
327 string const & format_name) const
329 if (filename.empty() || !filename.exists()) {
330 Alert::error(_("Cannot view file"),
331 bformat(_("File does not exist: %1$s"),
332 from_utf8(filename.absFileName())));
336 Format const * format = getFormat(format_name);
337 if (format && format->viewer().empty() &&
338 format->isChildFormat())
339 format = getFormat(format->parentFormat());
340 if (!format || format->viewer().empty()) {
341 // FIXME: I believe this is the wrong place to show alerts, it should be done
342 // by the caller (this should be "utility" code)
343 Alert::error(_("Cannot view file"),
344 bformat(_("No information for viewing %1$s"),
345 prettyName(format_name)));
349 if (format->viewer() == "auto") {
350 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
353 Alert::error(_("Cannot view file"),
354 bformat(_("Auto-view file %1$s failed"),
355 from_utf8(filename.absFileName())));
360 string command = libScriptSearch(format->viewer());
362 if (format_name == "dvi" &&
363 !lyxrc.view_dvi_paper_option.empty()) {
364 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
365 if (!paper_size.empty()) {
366 command += ' ' + lyxrc.view_dvi_paper_option;
367 command += ' ' + paper_size;
368 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
369 buffer.params().papersize != PAPER_CUSTOM)
374 if (!contains(command, token_from_format))
375 command += ' ' + token_from_format;
377 command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
378 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
379 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
380 LYXERR(Debug::FILES, "Executing command: " << command);
381 // FIXME UNICODE utf8 can be wrong for files
382 buffer.message(_("Executing command: ") + from_utf8(command));
384 PathChanger p(filename.onlyPath());
386 one.startscript(Systemcall::DontWait, command, buffer.filePath());
388 // we can't report any sort of error, since we aren't waiting
393 bool Formats::edit(Buffer const & buffer, FileName const & filename,
394 string const & format_name) const
396 if (filename.empty() || !filename.exists()) {
397 Alert::error(_("Cannot edit file"),
398 bformat(_("File does not exist: %1$s"),
399 from_utf8(filename.absFileName())));
403 // LinkBack files look like PDF, but have the .linkback extension
404 string const ext = getExtension(filename.absFileName());
405 if (format_name == "pdf" && ext == "linkback") {
406 #ifdef USE_MACOSX_PACKAGING
407 return editLinkBackFile(filename.absFileName().c_str());
409 Alert::error(_("Cannot edit file"),
410 _("LinkBack files can only be edited on Apple Mac OSX."));
412 #endif // USE_MACOSX_PACKAGING
415 Format const * format = getFormat(format_name);
416 if (format && format->editor().empty() &&
417 format->isChildFormat())
418 format = getFormat(format->parentFormat());
419 if (!format || format->editor().empty()) {
420 // FIXME: I believe this is the wrong place to show alerts, it should
421 // be done by the caller (this should be "utility" code)
422 Alert::error(_("Cannot edit file"),
423 bformat(_("No information for editing %1$s"),
424 prettyName(format_name)));
429 if (format->editor() == "auto") {
430 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
433 Alert::error(_("Cannot edit file"),
434 bformat(_("Auto-edit file %1$s failed"),
435 from_utf8(filename.absFileName())));
440 string command = format->editor();
442 if (!contains(command, token_from_format))
443 command += ' ' + token_from_format;
445 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
446 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
447 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
448 LYXERR(Debug::FILES, "Executing command: " << command);
449 // FIXME UNICODE utf8 can be wrong for files
450 buffer.message(_("Executing command: ") + from_utf8(command));
453 one.startscript(Systemcall::DontWait, command, buffer.filePath());
455 // we can't report any sort of error, since we aren't waiting
460 docstring const Formats::prettyName(string const & name) const
462 Format const * format = getFormat(name);
464 return from_utf8(format->prettyname());
466 return from_utf8(name);
470 string const Formats::extension(string const & name) const
472 Format const * format = getFormat(name);
474 return format->extension();
480 string const Formats::extensions(string const & name) const
482 Format const * format = getFormat(name);
484 return format->extensions();
491 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
493 FlavorTranslator initFlavorTranslator()
495 FlavorTranslator f(OutputParams::LATEX, "latex");
496 f.addPair(OutputParams::DVILUATEX, "dviluatex");
497 f.addPair(OutputParams::LUATEX, "luatex");
498 f.addPair(OutputParams::PDFLATEX, "pdflatex");
499 f.addPair(OutputParams::XETEX, "xetex");
500 f.addPair(OutputParams::XML, "docbook-xml");
501 f.addPair(OutputParams::HTML, "xhtml");
502 f.addPair(OutputParams::TEXT, "text");
507 FlavorTranslator const & flavorTranslator()
509 static FlavorTranslator translator = initFlavorTranslator();
515 std::string flavor2format(OutputParams::FLAVOR flavor)
517 return flavorTranslator().find(flavor);
521 /* Not currently needed, but I'll leave the code in case it is.
522 OutputParams::FLAVOR format2flavor(std::string fmt)
524 return flavorTranslator().find(fmt);
529 Formats system_formats;