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 "lyx_cb.h" // for ShowMessage() ... to be removed?
19 #include "frontends/Alert.h" //to be removed?
21 #include "support/filetools.h"
22 #include "support/path.h"
23 #include "support/systemcall.h"
24 #include "support/lyxfunctional.h"
29 string const token_from("$$i");
30 string const token_path("$$p");
34 bool operator<(Format const & a, Format const & b)
36 // use the compare_ascii_no_case instead of compare_no_case,
37 // because in turkish, 'i' is not the lowercase version of 'I',
38 // and thus turkish locale breaks parsing of tags.
40 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
43 Format::Format(string const & n, string const & e, string const & p,
44 string const & s, string const & v): name_(n),
52 bool Format::dummy() const
54 return extension().empty();
58 bool Format::isChildFormat() const
62 return isdigit(name_[name_.length() - 1]);
66 string const Format::parentFormat() const
68 return name_.substr(0, name_.length() - 1);
72 // This method should return a reference, and throw an exception
73 // if the format named name cannot be found (Lgb)
74 Format const * Formats::getFormat(string const & name) const
76 FormatList::const_iterator cit =
77 find_if(formatlist.begin(), formatlist.end(),
78 lyx::compare_memfun(&Format::name, name));
79 if (cit != formatlist.end())
86 int Formats::getNumber(string const & name) const
88 FormatList::const_iterator cit =
89 find_if(formatlist.begin(), formatlist.end(),
90 lyx::compare_memfun(&Format::name, name));
91 if (cit != formatlist.end())
92 return cit - formatlist.begin();
98 void Formats::add(string const & name)
100 if (!getFormat(name))
101 add(name, name, name, string());
105 void Formats::add(string const & name, string const & extension,
106 string const & prettyname, string const & shortcut)
108 FormatList::iterator it =
109 find_if(formatlist.begin(), formatlist.end(),
110 lyx::compare_memfun(&Format::name, name));
111 if (it == formatlist.end())
112 formatlist.push_back(Format(name, extension, prettyname,
115 string viewer = it->viewer();
116 *it = Format(name, extension, prettyname, shortcut, viewer);
121 void Formats::erase(string const & name)
123 FormatList::iterator it =
124 find_if(formatlist.begin(), formatlist.end(),
125 lyx::compare_memfun(&Format::name, name));
126 if (it != formatlist.end())
127 formatlist.erase(it);
133 std::sort(formatlist.begin(), formatlist.end());
137 void Formats::setViewer(string const & name, string const & command)
140 FormatList::iterator it =
141 find_if(formatlist.begin(), formatlist.end(),
142 lyx::compare_memfun(&Format::name, name));
143 if (it != formatlist.end())
144 it->setViewer(command);
148 bool Formats::view(Buffer const * buffer, string const & filename,
149 string const & format_name) const
151 if (filename.empty())
154 Format const * format = getFormat(format_name);
155 if (format && format->viewer().empty() &&
156 format->isChildFormat())
157 format = getFormat(format->parentFormat());
158 if (!format || format->viewer().empty()) {
159 // I believe this is the wrong place to show alerts, it should be done by
160 // the caller (this should be "utility" code
161 Alert::error(_("Cannot view file"),
162 bformat(_("No information for viewing %1$s"),
163 prettyName(format_name)));
167 string command = format->viewer();
169 if (format_name == "dvi" &&
170 !lyxrc.view_dvi_paper_option.empty()) {
171 command += ' ' + lyxrc.view_dvi_paper_option;
172 string paper_size = papersize(buffer);
173 if (paper_size == "letter")
175 command += ' ' + paper_size;
176 if (buffer->params.orientation
177 == BufferParams::ORIENTATION_LANDSCAPE)
181 if (!contains(command, token_from))
182 command += ' ' + token_from;
184 command = subst(command, token_from,
185 QuoteName(OnlyFilename(filename)));
186 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
188 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
189 ShowMessage(buffer, _("Executing command:"), command);
191 Path p(OnlyPath(filename));
193 int const res = one.startscript(Systemcall::DontWait, command);
196 Alert::error(_("Cannot view file"),
197 bformat(_("An error occurred whilst running %1$s"),
198 command.substr(0, 50)));
205 string const Formats::prettyName(string const & name) const
207 Format const * format = getFormat(name);
209 return format->prettyname();
215 string const Formats::extension(string const & name) const
217 Format const * format = getFormat(name);
219 return format->extension();
225 string const papersize(Buffer const * buffer)
227 char real_papersize = buffer->params.papersize;
228 if (real_papersize == BufferParams::PAPER_DEFAULT)
229 real_papersize = lyxrc.default_papersize;
231 switch (real_papersize) {
232 case BufferParams::PAPER_A3PAPER:
234 case BufferParams::PAPER_A4PAPER:
236 case BufferParams::PAPER_A5PAPER:
238 case BufferParams::PAPER_B5PAPER:
240 case BufferParams::PAPER_EXECUTIVEPAPER:
242 case BufferParams::PAPER_LEGALPAPER:
244 case BufferParams::PAPER_USLETTER:
253 Formats system_formats;