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/Systemcall.h"
27 #include "support/textutils.h"
31 // FIXME: Q_WS_MACX is not available, it's in Qt
32 #ifdef USE_MACOSX_PACKAGING
33 #include "support/linkback/LinkBackProxy.h"
37 using namespace lyx::support;
41 namespace Alert = frontend::Alert;
42 namespace os = support::os;
46 string const token_from_format("$$i");
47 string const token_path_format("$$p");
48 string const token_socket_format("$$a");
51 class FormatNamesEqual : public unary_function<Format, bool> {
53 FormatNamesEqual(string const & name)
55 bool operator()(Format const & f) const
57 return f.name() == name_;
64 class FormatExtensionsEqual : public unary_function<Format, bool> {
66 FormatExtensionsEqual(string const & extension)
67 : extension_(extension) {}
68 bool operator()(Format const & f) const
70 return f.extension() == extension_;
78 bool operator<(Format const & a, Format const & b)
80 // use the compare_ascii_no_case instead of compare_no_case,
81 // because in turkish, 'i' is not the lowercase version of 'I',
82 // and thus turkish locale breaks parsing of tags.
84 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
88 Format::Format(string const & n, string const & e, string const & p,
89 string const & s, string const & v, string const & ed,
91 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
92 editor_(ed), flags_(flags)
96 bool Format::dummy() const
98 return extension().empty();
102 bool Format::isChildFormat() const
106 return isDigitASCII(name_[name_.length() - 1]);
110 string const Format::parentFormat() const
112 return name_.substr(0, name_.length() - 1);
116 // This method should return a reference, and throw an exception
117 // if the format named name cannot be found (Lgb)
118 Format const * Formats::getFormat(string const & name) const
120 FormatList::const_iterator cit =
121 find_if(formatlist.begin(), formatlist.end(),
122 FormatNamesEqual(name));
123 if (cit != formatlist.end())
130 string Formats::getFormatFromFile(FileName const & filename) const
132 if (filename.empty())
135 string const format = filename.guessFormatFromContents();
139 // try to find a format from the file extension.
140 string const ext = getExtension(filename.absFileName());
142 // this is ambigous if two formats have the same extension,
143 // but better than nothing
144 Formats::const_iterator cit =
145 find_if(formatlist.begin(), formatlist.end(),
146 FormatExtensionsEqual(ext));
147 if (cit != formats.end()) {
148 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
149 << ext << " -> " << cit->name());
157 static string fixCommand(string const & cmd, string const & ext,
158 os::auto_open_mode mode)
160 // configure.py says we do not want a viewer/editor
164 // Does the OS manage this format?
165 if (os::canAutoOpenFile(ext, mode))
168 // if configure.py found nothing, clear the command
169 if (token(cmd, ' ', 0) == "auto")
172 // use the command found by configure.py
177 void Formats::setAutoOpen()
179 FormatList::iterator fit = formatlist.begin();
180 FormatList::iterator const fend = formatlist.end();
181 for ( ; fit != fend ; ++fit) {
182 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
183 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
188 int Formats::getNumber(string const & name) const
190 FormatList::const_iterator cit =
191 find_if(formatlist.begin(), formatlist.end(),
192 FormatNamesEqual(name));
193 if (cit != formatlist.end())
194 return distance(formatlist.begin(), cit);
200 void Formats::add(string const & name)
202 if (!getFormat(name))
203 add(name, name, name, string(), string(), string(),
208 void Formats::add(string const & name, string const & extension,
209 string const & prettyname, string const & shortcut,
210 string const & viewer, string const & editor,
213 FormatList::iterator it =
214 find_if(formatlist.begin(), formatlist.end(),
215 FormatNamesEqual(name));
216 if (it == formatlist.end())
217 formatlist.push_back(Format(name, extension, prettyname,
218 shortcut, viewer, editor, flags));
220 *it = Format(name, extension, prettyname, shortcut, viewer,
225 void Formats::erase(string const & name)
227 FormatList::iterator it =
228 find_if(formatlist.begin(), formatlist.end(),
229 FormatNamesEqual(name));
230 if (it != formatlist.end())
231 formatlist.erase(it);
237 std::sort(formatlist.begin(), formatlist.end());
241 void Formats::setViewer(string const & name, string const & command)
244 FormatList::iterator it =
245 find_if(formatlist.begin(), formatlist.end(),
246 FormatNamesEqual(name));
247 if (it != formatlist.end())
248 it->setViewer(command);
252 void Formats::setEditor(string const & name, string const & command)
255 FormatList::iterator it =
256 find_if(formatlist.begin(), formatlist.end(),
257 FormatNamesEqual(name));
258 if (it != formatlist.end())
259 it->setEditor(command);
263 bool Formats::view(Buffer const & buffer, FileName const & filename,
264 string const & format_name) const
266 if (filename.empty() || !filename.exists()) {
267 Alert::error(_("Cannot view file"),
268 bformat(_("File does not exist: %1$s"),
269 from_utf8(filename.absFileName())));
273 Format const * format = getFormat(format_name);
274 if (format && format->viewer().empty() &&
275 format->isChildFormat())
276 format = getFormat(format->parentFormat());
277 if (!format || format->viewer().empty()) {
278 // FIXME: I believe this is the wrong place to show alerts, it should be done
279 // by the caller (this should be "utility" code)
280 Alert::error(_("Cannot view file"),
281 bformat(_("No information for viewing %1$s"),
282 prettyName(format_name)));
286 if (format->viewer() == "auto") {
287 if (os::autoOpenFile(filename.absFileName(), os::VIEW))
290 Alert::error(_("Cannot view file"),
291 bformat(_("Auto-view file %1$s failed"),
292 from_utf8(filename.absFileName())));
297 string command = libScriptSearch(format->viewer());
299 if (format_name == "dvi" &&
300 !lyxrc.view_dvi_paper_option.empty()) {
301 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
302 if (!paper_size.empty()) {
303 command += ' ' + lyxrc.view_dvi_paper_option;
304 command += ' ' + paper_size;
305 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
306 buffer.params().papersize != PAPER_CUSTOM)
311 if (!contains(command, token_from_format))
312 command += ' ' + token_from_format;
314 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
315 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
316 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
317 LYXERR(Debug::FILES, "Executing command: " << command);
318 // FIXME UNICODE utf8 can be wrong for files
319 buffer.message(_("Executing command: ") + from_utf8(command));
322 one.startscript(Systemcall::DontWait, command);
324 // we can't report any sort of error, since we aren't waiting
329 bool Formats::edit(Buffer const & buffer, FileName const & filename,
330 string const & format_name) const
332 if (filename.empty() || !filename.exists()) {
333 Alert::error(_("Cannot edit file"),
334 bformat(_("File does not exist: %1$s"),
335 from_utf8(filename.absFileName())));
339 // LinkBack files look like PDF, but have the .linkback extension
340 string const ext = getExtension(filename.absFileName());
341 if (format_name == "pdf" && ext == "linkback") {
342 #ifdef USE_MACOSX_PACKAGING
343 return editLinkBackFile(filename.absFileName().c_str());
345 Alert::error(_("Cannot edit file"),
346 _("LinkBack files can only be edited on Apple Mac OSX."));
348 #endif // USE_MACOSX_PACKAGING
351 Format const * format = getFormat(format_name);
352 if (format && format->editor().empty() &&
353 format->isChildFormat())
354 format = getFormat(format->parentFormat());
355 if (!format || format->editor().empty()) {
356 // FIXME: I believe this is the wrong place to show alerts, it should
357 // be done by the caller (this should be "utility" code)
358 Alert::error(_("Cannot edit file"),
359 bformat(_("No information for editing %1$s"),
360 prettyName(format_name)));
365 if (format->editor() == "auto") {
366 if (os::autoOpenFile(filename.absFileName(), os::EDIT))
369 Alert::error(_("Cannot edit file"),
370 bformat(_("Auto-edit file %1$s failed"),
371 from_utf8(filename.absFileName())));
376 string command = format->editor();
378 if (!contains(command, token_from_format))
379 command += ' ' + token_from_format;
381 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
382 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
383 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
384 LYXERR(Debug::FILES, "Executing command: " << command);
385 // FIXME UNICODE utf8 can be wrong for files
386 buffer.message(_("Executing command: ") + from_utf8(command));
389 one.startscript(Systemcall::DontWait, command);
391 // we can't report any sort of error, since we aren't waiting
396 docstring const Formats::prettyName(string const & name) const
398 Format const * format = getFormat(name);
400 return from_utf8(format->prettyname());
402 return from_utf8(name);
406 string const Formats::extension(string const & name) const
408 Format const * format = getFormat(name);
410 return format->extension();
420 Formats system_formats;