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"
30 string const token_from("$$i");
31 string const token_path("$$p");
35 bool operator<(Format const & a, Format const & b)
37 // use the compare_ascii_no_case instead of compare_no_case,
38 // because in turkish, 'i' is not the lowercase version of 'I',
39 // and thus turkish locale breaks parsing of tags.
41 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
44 Format::Format(string const & n, string const & e, string const & p,
45 string const & s, string const & v): name_(n),
53 bool Format::dummy() const
55 return extension().empty();
59 bool Format::isChildFormat() const
63 return isdigit(name_[name_.length() - 1]);
67 string const Format::parentFormat() const
69 return name_.substr(0, name_.length() - 1);
73 // This method should return a reference, and throw an exception
74 // if the format named name cannot be found (Lgb)
75 Format const * Formats::getFormat(string const & name) const
77 FormatList::const_iterator cit =
78 find_if(formatlist.begin(), formatlist.end(),
79 lyx::compare_memfun(&Format::name, name));
80 if (cit != formatlist.end())
87 int Formats::getNumber(string const & name) const
89 FormatList::const_iterator cit =
90 find_if(formatlist.begin(), formatlist.end(),
91 lyx::compare_memfun(&Format::name, name));
92 if (cit != formatlist.end())
93 return cit - formatlist.begin();
99 void Formats::add(string const & name)
101 if (!getFormat(name))
102 add(name, name, name, string());
106 void Formats::add(string const & name, string const & extension,
107 string const & prettyname, string const & shortcut)
109 FormatList::iterator it =
110 find_if(formatlist.begin(), formatlist.end(),
111 lyx::compare_memfun(&Format::name, name));
112 if (it == formatlist.end())
113 formatlist.push_back(Format(name, extension, prettyname,
116 string viewer = it->viewer();
117 *it = Format(name, extension, prettyname, shortcut, viewer);
122 void Formats::erase(string const & name)
124 FormatList::iterator it =
125 find_if(formatlist.begin(), formatlist.end(),
126 lyx::compare_memfun(&Format::name, name));
127 if (it != formatlist.end())
128 formatlist.erase(it);
134 std::sort(formatlist.begin(), formatlist.end());
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()) {
160 // I believe this is the wrong place to show alerts, it should be done by
161 // the caller (this should be "utility" code
162 Alert::error(_("Cannot view file"),
163 bformat(_("No information for viewing %1$s"),
164 prettyName(format_name)));
168 string command = format->viewer();
170 if (format_name == "dvi" &&
171 !lyxrc.view_dvi_paper_option.empty()) {
172 command += ' ' + lyxrc.view_dvi_paper_option;
173 string paper_size = papersize(buffer);
174 if (paper_size == "letter")
176 command += ' ' + paper_size;
177 if (buffer->params.orientation
178 == BufferParams::ORIENTATION_LANDSCAPE)
182 if (!contains(command, token_from))
183 command += ' ' + token_from;
185 command = subst(command, token_from,
186 QuoteName(OnlyFilename(filename)));
187 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
189 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
190 ShowMessage(buffer, _("Executing command:"), command);
192 Path p(OnlyPath(filename));
194 int const res = one.startscript(Systemcall::DontWait, command);
197 Alert::error(_("Cannot view file"),
198 bformat(_("An error occurred whilst running %1$s"),
199 command.substr(0, 50)));
206 string const Formats::prettyName(string const & name) const
208 Format const * format = getFormat(name);
210 return format->prettyname();
216 string const Formats::extension(string const & name) const
218 Format const * format = getFormat(name);
220 return format->extension();
226 string const papersize(Buffer const * buffer)
228 char real_papersize = buffer->params.papersize;
229 if (real_papersize == BufferParams::PAPER_DEFAULT)
230 real_papersize = lyxrc.default_papersize;
232 switch (real_papersize) {
233 case BufferParams::PAPER_A3PAPER:
235 case BufferParams::PAPER_A4PAPER:
237 case BufferParams::PAPER_A5PAPER:
239 case BufferParams::PAPER_B5PAPER:
241 case BufferParams::PAPER_EXECUTIVEPAPER:
243 case BufferParams::PAPER_LEGALPAPER:
245 case BufferParams::PAPER_USLETTER:
254 Formats system_formats;