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
20 #include "frontends/Alert.h" //to be removed?
22 #include "support/lstrings.h"
23 #include "support/filetools.h"
24 #include "support/path.h"
25 #include "support/systemcall.h"
26 #include "support/lyxfunctional.h"
28 using namespace lyx::support;
33 string const token_from("$$i");
34 string const token_path("$$p");
38 bool operator<(Format const & a, Format const & b)
40 // use the compare_ascii_no_case instead of compare_no_case,
41 // because in turkish, 'i' is not the lowercase version of 'I',
42 // and thus turkish locale breaks parsing of tags.
44 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
47 Format::Format(string const & n, string const & e, string const & p,
48 string const & s, string const & v): name_(n),
56 bool Format::dummy() const
58 return extension().empty();
62 bool Format::isChildFormat() const
66 return isdigit(name_[name_.length() - 1]);
70 string const Format::parentFormat() const
72 return name_.substr(0, name_.length() - 1);
76 // This method should return a reference, and throw an exception
77 // if the format named name cannot be found (Lgb)
78 Format const * Formats::getFormat(string const & name) const
80 FormatList::const_iterator cit =
81 find_if(formatlist.begin(), formatlist.end(),
82 lyx::compare_memfun(&Format::name, name));
83 if (cit != formatlist.end())
90 int Formats::getNumber(string const & name) const
92 FormatList::const_iterator cit =
93 find_if(formatlist.begin(), formatlist.end(),
94 lyx::compare_memfun(&Format::name, name));
95 if (cit != formatlist.end())
96 return cit - formatlist.begin();
102 void Formats::add(string const & name)
104 if (!getFormat(name))
105 add(name, name, name, string());
109 void Formats::add(string const & name, string const & extension,
110 string const & prettyname, string const & shortcut)
112 FormatList::iterator it =
113 find_if(formatlist.begin(), formatlist.end(),
114 lyx::compare_memfun(&Format::name, name));
115 if (it == formatlist.end())
116 formatlist.push_back(Format(name, extension, prettyname,
119 string viewer = it->viewer();
120 *it = Format(name, extension, prettyname, shortcut, viewer);
125 void Formats::erase(string const & name)
127 FormatList::iterator it =
128 find_if(formatlist.begin(), formatlist.end(),
129 lyx::compare_memfun(&Format::name, name));
130 if (it != formatlist.end())
131 formatlist.erase(it);
137 std::sort(formatlist.begin(), formatlist.end());
141 void Formats::setViewer(string const & name, string const & command)
144 FormatList::iterator it =
145 find_if(formatlist.begin(), formatlist.end(),
146 lyx::compare_memfun(&Format::name, name));
147 if (it != formatlist.end())
148 it->setViewer(command);
152 bool Formats::view(Buffer const * buffer, string const & filename,
153 string const & format_name) const
155 if (filename.empty())
158 Format const * format = getFormat(format_name);
159 if (format && format->viewer().empty() &&
160 format->isChildFormat())
161 format = getFormat(format->parentFormat());
162 if (!format || format->viewer().empty()) {
163 // I believe this is the wrong place to show alerts, it should be done by
164 // the caller (this should be "utility" code
165 Alert::error(_("Cannot view file"),
166 bformat(_("No information for viewing %1$s"),
167 prettyName(format_name)));
171 string command = format->viewer();
173 if (format_name == "dvi" &&
174 !lyxrc.view_dvi_paper_option.empty()) {
175 command += ' ' + lyxrc.view_dvi_paper_option;
176 string paper_size = papersize(buffer);
177 if (paper_size == "letter")
179 command += ' ' + paper_size;
180 if (buffer->params.orientation
181 == BufferParams::ORIENTATION_LANDSCAPE)
185 if (!contains(command, token_from))
186 command += ' ' + token_from;
188 command = subst(command, token_from,
189 QuoteName(OnlyFilename(filename)));
190 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
192 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
193 buffer->message(_("Executing command: ") + command);
195 Path p(OnlyPath(filename));
197 int const res = one.startscript(Systemcall::DontWait, command);
200 Alert::error(_("Cannot view file"),
201 bformat(_("An error occurred whilst running %1$s"),
202 command.substr(0, 50)));
209 string const Formats::prettyName(string const & name) const
211 Format const * format = getFormat(name);
213 return format->prettyname();
219 string const Formats::extension(string const & name) const
221 Format const * format = getFormat(name);
223 return format->extension();
229 string const papersize(Buffer const * buffer)
231 char real_papersize = buffer->params.papersize;
232 if (real_papersize == BufferParams::PAPER_DEFAULT)
233 real_papersize = lyxrc.default_papersize;
235 switch (real_papersize) {
236 case BufferParams::PAPER_A3PAPER:
238 case BufferParams::PAPER_A4PAPER:
240 case BufferParams::PAPER_A5PAPER:
242 case BufferParams::PAPER_B5PAPER:
244 case BufferParams::PAPER_EXECUTIVEPAPER:
246 case BufferParams::PAPER_LEGALPAPER:
248 case BufferParams::PAPER_USLETTER:
257 Formats system_formats;