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"
20 #include "frontends/Alert.h" //to be removed?
22 #include "support/filetools.h"
23 #include "support/path.h"
24 #include "support/systemcall.h"
25 #include "support/lyxfunctional.h"
27 using namespace lyx::support;
32 string const token_from("$$i");
33 string const token_path("$$p");
37 bool operator<(Format const & a, Format const & b)
39 // use the compare_ascii_no_case instead of compare_no_case,
40 // because in turkish, 'i' is not the lowercase version of 'I',
41 // and thus turkish locale breaks parsing of tags.
43 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
46 Format::Format(string const & n, string const & e, string const & p,
47 string const & s, string const & v): name_(n),
55 bool Format::dummy() const
57 return extension().empty();
61 bool Format::isChildFormat() const
65 return isdigit(name_[name_.length() - 1]);
69 string const Format::parentFormat() const
71 return name_.substr(0, name_.length() - 1);
75 // This method should return a reference, and throw an exception
76 // if the format named name cannot be found (Lgb)
77 Format const * Formats::getFormat(string const & name) const
79 FormatList::const_iterator cit =
80 find_if(formatlist.begin(), formatlist.end(),
81 lyx::compare_memfun(&Format::name, name));
82 if (cit != formatlist.end())
89 int Formats::getNumber(string const & name) const
91 FormatList::const_iterator cit =
92 find_if(formatlist.begin(), formatlist.end(),
93 lyx::compare_memfun(&Format::name, name));
94 if (cit != formatlist.end())
95 return cit - formatlist.begin();
101 void Formats::add(string const & name)
103 if (!getFormat(name))
104 add(name, name, name, string());
108 void Formats::add(string const & name, string const & extension,
109 string const & prettyname, string const & shortcut)
111 FormatList::iterator it =
112 find_if(formatlist.begin(), formatlist.end(),
113 lyx::compare_memfun(&Format::name, name));
114 if (it == formatlist.end())
115 formatlist.push_back(Format(name, extension, prettyname,
118 string viewer = it->viewer();
119 *it = Format(name, extension, prettyname, shortcut, viewer);
124 void Formats::erase(string const & name)
126 FormatList::iterator it =
127 find_if(formatlist.begin(), formatlist.end(),
128 lyx::compare_memfun(&Format::name, name));
129 if (it != formatlist.end())
130 formatlist.erase(it);
136 std::sort(formatlist.begin(), formatlist.end());
140 void Formats::setViewer(string const & name, string const & command)
143 FormatList::iterator it =
144 find_if(formatlist.begin(), formatlist.end(),
145 lyx::compare_memfun(&Format::name, name));
146 if (it != formatlist.end())
147 it->setViewer(command);
151 bool Formats::view(Buffer const & buffer, string const & filename,
152 string const & format_name) const
154 if (filename.empty())
157 Format const * format = getFormat(format_name);
158 if (format && format->viewer().empty() &&
159 format->isChildFormat())
160 format = getFormat(format->parentFormat());
161 if (!format || format->viewer().empty()) {
162 // I believe this is the wrong place to show alerts, it should be done by
163 // the caller (this should be "utility" code
164 Alert::error(_("Cannot view file"),
165 bformat(_("No information for viewing %1$s"),
166 prettyName(format_name)));
170 string command = format->viewer();
172 if (format_name == "dvi" &&
173 !lyxrc.view_dvi_paper_option.empty()) {
174 command += ' ' + lyxrc.view_dvi_paper_option;
175 string paper_size = buffer.params().paperSizeName();
176 if (paper_size == "letter")
178 command += ' ' + paper_size;
179 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
183 if (!contains(command, token_from))
184 command += ' ' + token_from;
186 command = subst(command, token_from,
187 QuoteName(OnlyFilename(filename)));
188 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
190 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
191 buffer.message(_("Executing command: ") + command);
193 Path p(OnlyPath(filename));
195 int const res = one.startscript(Systemcall::DontWait, command);
198 Alert::error(_("Cannot view file"),
199 bformat(_("An error occurred whilst running %1$s"),
200 command.substr(0, 50)));
207 string const Formats::prettyName(string const & name) const
209 Format const * format = getFormat(name);
211 return format->prettyname();
217 string const Formats::extension(string const & name) const
219 Format const * format = getFormat(name);
221 return format->extension();
231 Formats system_formats;