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
14 #include "lyx_cb.h" // for ShowMessage() ... to be removed?
18 #include "frontends/Alert.h" //to be removed?
20 #include "support/filetools.h"
21 #include "support/path.h"
22 #include "support/systemcall.h"
23 #include "support/lyxfunctional.h"
28 string const token_from("$$i");
29 string const token_path("$$p");
33 bool operator<(Format const & a, Format const & b)
35 // use the compare_ascii_no_case instead of compare_no_case,
36 // because in turkish, 'i' is not the lowercase version of 'I',
37 // and thus turkish locale breaks parsing of tags.
39 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
42 Format::Format(string const & n, string const & e, string const & p,
43 string const & s, string const & v): name_(n),
51 bool Format::dummy() const
53 return extension().empty();
57 bool Format::isChildFormat() const
61 return isdigit(name_[name_.length() - 1]);
65 string const Format::parentFormat() const
67 return name_.substr(0, name_.length() - 1);
71 // This method should return a reference, and throw an exception
72 // if the format named name cannot be found (Lgb)
73 Format const * Formats::getFormat(string const & name) const
75 FormatList::const_iterator cit =
76 find_if(formatlist.begin(), formatlist.end(),
77 lyx::compare_memfun(&Format::name, name));
78 if (cit != formatlist.end())
85 int Formats::getNumber(string const & name) const
87 FormatList::const_iterator cit =
88 find_if(formatlist.begin(), formatlist.end(),
89 lyx::compare_memfun(&Format::name, name));
90 if (cit != formatlist.end())
91 return cit - formatlist.begin();
97 void Formats::add(string const & name)
100 add(name, name, name, string());
104 void Formats::add(string const & name, string const & extension,
105 string const & prettyname, string const & shortcut)
107 FormatList::iterator it =
108 find_if(formatlist.begin(), formatlist.end(),
109 lyx::compare_memfun(&Format::name, name));
110 if (it == formatlist.end())
111 formatlist.push_back(Format(name, extension, prettyname,
114 string viewer = it->viewer();
115 *it = Format(name, extension, prettyname, shortcut, viewer);
120 void Formats::erase(string const & name)
122 FormatList::iterator it =
123 find_if(formatlist.begin(), formatlist.end(),
124 lyx::compare_memfun(&Format::name, name));
125 if (it != formatlist.end())
126 formatlist.erase(it);
132 cout << "here" << endl;
133 std::sort(formatlist.begin(), formatlist.end());
134 cout << "and here " << formatlist.size() << endl;
138 void Formats::setViewer(string const & name, string const & command)
141 FormatList::iterator it =
142 find_if(formatlist.begin(), formatlist.end(),
143 lyx::compare_memfun(&Format::name, name));
144 if (it != formatlist.end())
145 it->setViewer(command);
149 bool Formats::view(Buffer const * buffer, string const & filename,
150 string const & format_name) const
152 if (filename.empty())
155 Format const * format = getFormat(format_name);
156 if (format && format->viewer().empty() &&
157 format->isChildFormat())
158 format = getFormat(format->parentFormat());
159 if (!format || format->viewer().empty()) {
161 Alert::alert(_("Cannot view file"),
162 boost::io::str(boost::format(_("No information for viewing %1$s"))
163 % prettyName(format_name)));
165 Alert::alert(_("Cannot view file"),
166 _("No information for viewing ")
167 + prettyName(format_name));
172 string command = format->viewer();
174 if (format_name == "dvi" &&
175 !lyxrc.view_dvi_paper_option.empty()) {
176 command += ' ' + lyxrc.view_dvi_paper_option;
177 string paper_size = papersize(buffer);
178 if (paper_size == "letter")
180 command += ' ' + paper_size;
181 if (buffer->params.orientation
182 == BufferParams::ORIENTATION_LANDSCAPE)
186 if (!contains(command, token_from))
187 command += ' ' + token_from;
189 command = subst(command, token_from,
190 QuoteName(OnlyFilename(filename)));
191 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
193 lyxerr[Debug::FILES] << "Executing command: " << command << endl;
194 ShowMessage(buffer, _("Executing command:"), command);
196 Path p(OnlyPath(filename));
198 int const res = one.startscript(Systemcall::DontWait, command);
201 Alert::alert(_("Cannot view file"),
202 _("Error while executing"),
203 command.substr(0, 50));
210 string const Formats::prettyName(string const & name) const
212 Format const * format = getFormat(name);
214 return format->prettyname();
220 string const Formats::extension(string const & name) const
222 Format const * format = getFormat(name);
224 return format->extension();
230 string const papersize(Buffer const * buffer)
232 char real_papersize = buffer->params.papersize;
233 if (real_papersize == BufferParams::PAPER_DEFAULT)
234 real_papersize = lyxrc.default_papersize;
236 switch (real_papersize) {
237 case BufferParams::PAPER_A3PAPER:
239 case BufferParams::PAPER_A4PAPER:
241 case BufferParams::PAPER_A5PAPER:
243 case BufferParams::PAPER_B5PAPER:
245 case BufferParams::PAPER_EXECUTIVEPAPER:
247 case BufferParams::PAPER_LEGALPAPER:
249 case BufferParams::PAPER_USLETTER:
258 Formats system_formats;