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::viewURL(docstring const & url) {
264 Format const * format = getFormat("html");
268 string command = libScriptSearch(format->viewer());
270 if (!contains(command, token_from_format))
271 command += ' ' + token_from_format;
272 command = subst(command, token_from_format, quoteName(to_utf8(url)));
274 LYXERR(Debug::FILES, "Executing command: " << command);
277 one.startscript(Systemcall::DontWait, command);
279 // we can't report any sort of error, since we aren't waiting
284 bool Formats::view(Buffer const & buffer, FileName const & filename,
285 string const & format_name) const
287 if (filename.empty() || !filename.exists()) {
288 Alert::error(_("Cannot view file"),
289 bformat(_("File does not exist: %1$s"),
290 from_utf8(filename.absFileName())));
294 Format const * format = getFormat(format_name);
295 if (format && format->viewer().empty() &&
296 format->isChildFormat())
297 format = getFormat(format->parentFormat());
298 if (!format || format->viewer().empty()) {
299 // FIXME: I believe this is the wrong place to show alerts, it should be done
300 // by the caller (this should be "utility" code)
301 Alert::error(_("Cannot view file"),
302 bformat(_("No information for viewing %1$s"),
303 prettyName(format_name)));
307 if (format->viewer() == "auto") {
308 if (os::autoOpenFile(filename.absFileName(), os::VIEW))
311 Alert::error(_("Cannot view file"),
312 bformat(_("Auto-view file %1$s failed"),
313 from_utf8(filename.absFileName())));
318 string command = libScriptSearch(format->viewer());
320 if (format_name == "dvi" &&
321 !lyxrc.view_dvi_paper_option.empty()) {
322 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
323 if (!paper_size.empty()) {
324 command += ' ' + lyxrc.view_dvi_paper_option;
325 command += ' ' + paper_size;
326 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
327 buffer.params().papersize != PAPER_CUSTOM)
332 if (!contains(command, token_from_format))
333 command += ' ' + token_from_format;
335 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
336 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
337 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
338 LYXERR(Debug::FILES, "Executing command: " << command);
339 // FIXME UNICODE utf8 can be wrong for files
340 buffer.message(_("Executing command: ") + from_utf8(command));
343 one.startscript(Systemcall::DontWait, command);
345 // we can't report any sort of error, since we aren't waiting
350 bool Formats::edit(Buffer const & buffer, FileName const & filename,
351 string const & format_name) const
353 if (filename.empty() || !filename.exists()) {
354 Alert::error(_("Cannot edit file"),
355 bformat(_("File does not exist: %1$s"),
356 from_utf8(filename.absFileName())));
360 // LinkBack files look like PDF, but have the .linkback extension
361 string const ext = getExtension(filename.absFileName());
362 if (format_name == "pdf" && ext == "linkback") {
363 #ifdef USE_MACOSX_PACKAGING
364 return editLinkBackFile(filename.absFileName().c_str());
366 Alert::error(_("Cannot edit file"),
367 _("LinkBack files can only be edited on Apple Mac OSX."));
369 #endif // USE_MACOSX_PACKAGING
372 Format const * format = getFormat(format_name);
373 if (format && format->editor().empty() &&
374 format->isChildFormat())
375 format = getFormat(format->parentFormat());
376 if (!format || format->editor().empty()) {
377 // FIXME: I believe this is the wrong place to show alerts, it should
378 // be done by the caller (this should be "utility" code)
379 Alert::error(_("Cannot edit file"),
380 bformat(_("No information for editing %1$s"),
381 prettyName(format_name)));
386 if (format->editor() == "auto") {
387 if (os::autoOpenFile(filename.absFileName(), os::EDIT))
390 Alert::error(_("Cannot edit file"),
391 bformat(_("Auto-edit file %1$s failed"),
392 from_utf8(filename.absFileName())));
397 string command = format->editor();
399 if (!contains(command, token_from_format))
400 command += ' ' + token_from_format;
402 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
403 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
404 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
405 LYXERR(Debug::FILES, "Executing command: " << command);
406 // FIXME UNICODE utf8 can be wrong for files
407 buffer.message(_("Executing command: ") + from_utf8(command));
410 one.startscript(Systemcall::DontWait, command);
412 // we can't report any sort of error, since we aren't waiting
417 docstring const Formats::prettyName(string const & name) const
419 Format const * format = getFormat(name);
421 return from_utf8(format->prettyname());
423 return from_utf8(name);
427 string const Formats::extension(string const & name) const
429 Format const * format = getFormat(name);
431 return format->extension();
441 Formats system_formats;