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"
19 #include "lyxsocket.h"
21 #include "frontends/Alert.h" //to be removed?
23 #include "support/filetools.h"
24 #include "support/path.h"
25 #include "support/systemcall.h"
27 using lyx::support::bformat;
28 using lyx::support::compare_ascii_no_case;
29 using lyx::support::contains;
30 using lyx::support::OnlyFilename;
31 using lyx::support::OnlyPath;
32 using lyx::support::Path;
33 using lyx::support::QuoteName;
34 using lyx::support::subst;
35 using lyx::support::Systemcall;
39 extern LyXServerSocket * lyxsocket;
43 string const token_from("$$i");
44 string const token_path("$$p");
45 string const token_socket("$$a");
48 class FormatNamesEqual : public std::unary_function<Format, bool> {
50 FormatNamesEqual(string const & name)
52 bool operator()(Format const & f) const
54 return f.name() == name_;
62 bool operator<(Format const & a, Format const & b)
64 // use the compare_ascii_no_case instead of compare_no_case,
65 // because in turkish, 'i' is not the lowercase version of 'I',
66 // and thus turkish locale breaks parsing of tags.
68 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
71 Format::Format(string const & n, string const & e, string const & p,
72 string const & s, string const & v)
73 : name_(n), extension_(e), prettyname_(p),shortcut_(s), viewer_(v)
77 bool Format::dummy() const
79 return extension().empty();
83 bool Format::isChildFormat() const
87 return isdigit(name_[name_.length() - 1]);
91 string const Format::parentFormat() const
93 return name_.substr(0, name_.length() - 1);
97 // This method should return a reference, and throw an exception
98 // if the format named name cannot be found (Lgb)
99 Format const * Formats::getFormat(string const & name) const
101 FormatList::const_iterator cit =
102 find_if(formatlist.begin(), formatlist.end(),
103 FormatNamesEqual(name));
104 if (cit != formatlist.end())
111 int Formats::getNumber(string const & name) const
113 FormatList::const_iterator cit =
114 find_if(formatlist.begin(), formatlist.end(),
115 FormatNamesEqual(name));
116 if (cit != formatlist.end())
117 return distance(formatlist.begin(), cit);
123 void Formats::add(string const & name)
125 if (!getFormat(name))
126 add(name, name, name, string());
130 void Formats::add(string const & name, string const & extension,
131 string const & prettyname, string const & shortcut)
133 FormatList::iterator it =
134 find_if(formatlist.begin(), formatlist.end(),
135 FormatNamesEqual(name));
136 if (it == formatlist.end())
137 formatlist.push_back(Format(name, extension, prettyname,
140 string viewer = it->viewer();
141 *it = Format(name, extension, prettyname, shortcut, viewer);
146 void Formats::erase(string const & name)
148 FormatList::iterator it =
149 find_if(formatlist.begin(), formatlist.end(),
150 FormatNamesEqual(name));
151 if (it != formatlist.end())
152 formatlist.erase(it);
158 std::sort(formatlist.begin(), formatlist.end());
162 void Formats::setViewer(string const & name, string const & command)
165 FormatList::iterator it =
166 find_if(formatlist.begin(), formatlist.end(),
167 FormatNamesEqual(name));
168 if (it != formatlist.end())
169 it->setViewer(command);
173 bool Formats::view(Buffer const & buffer, string const & filename,
174 string const & format_name) const
176 if (filename.empty())
179 Format const * format = getFormat(format_name);
180 if (format && format->viewer().empty() &&
181 format->isChildFormat())
182 format = getFormat(format->parentFormat());
183 if (!format || format->viewer().empty()) {
184 // I believe this is the wrong place to show alerts, it should be done by
185 // the caller (this should be "utility" code
186 Alert::error(_("Cannot view file"),
187 bformat(_("No information for viewing %1$s"),
188 prettyName(format_name)));
192 string command = format->viewer();
194 if (format_name == "dvi" &&
195 !lyxrc.view_dvi_paper_option.empty()) {
196 command += ' ' + lyxrc.view_dvi_paper_option;
197 string paper_size = buffer.params().paperSizeName();
198 if (paper_size == "letter")
200 command += ' ' + paper_size;
201 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
205 if (!contains(command, token_from))
206 command += ' ' + token_from;
208 command = subst(command, token_from,
209 QuoteName(OnlyFilename(filename)));
210 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
211 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
212 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
213 buffer.message(_("Executing command: ") + command);
215 Path p(OnlyPath(filename));
217 int const res = one.startscript(Systemcall::DontWait, command);
220 Alert::error(_("Cannot view file"),
221 bformat(_("An error occurred whilst running %1$s"),
222 command.substr(0, 50)));
229 string const Formats::prettyName(string const & name) const
231 Format const * format = getFormat(name);
233 return format->prettyname();
239 string const Formats::extension(string const & name) const
241 Format const * format = getFormat(name);
243 return format->extension();
253 Formats system_formats;