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;
40 extern LyXServerSocket * lyxsocket;
44 string const token_from("$$i");
45 string const token_path("$$p");
46 string const token_socket("$$a");
49 class FormatNamesEqual : public std::unary_function<Format, bool> {
51 FormatNamesEqual(string const & name)
53 bool operator()(Format const & f) const
55 return f.name() == name_;
63 bool operator<(Format const & a, Format const & b)
65 // use the compare_ascii_no_case instead of compare_no_case,
66 // because in turkish, 'i' is not the lowercase version of 'I',
67 // and thus turkish locale breaks parsing of tags.
69 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
72 Format::Format(string const & n, string const & e, string const & p,
73 string const & s, string const & v)
74 : name_(n), extension_(e), prettyname_(p),shortcut_(s), viewer_(v)
78 bool Format::dummy() const
80 return extension().empty();
84 bool Format::isChildFormat() const
88 return isdigit(name_[name_.length() - 1]);
92 string const Format::parentFormat() const
94 return name_.substr(0, name_.length() - 1);
98 // This method should return a reference, and throw an exception
99 // if the format named name cannot be found (Lgb)
100 Format const * Formats::getFormat(string const & name) const
102 FormatList::const_iterator cit =
103 find_if(formatlist.begin(), formatlist.end(),
104 FormatNamesEqual(name));
105 if (cit != formatlist.end())
112 int Formats::getNumber(string const & name) const
114 FormatList::const_iterator cit =
115 find_if(formatlist.begin(), formatlist.end(),
116 FormatNamesEqual(name));
117 if (cit != formatlist.end())
118 return distance(formatlist.begin(), cit);
124 void Formats::add(string const & name)
126 if (!getFormat(name))
127 add(name, name, name, string());
131 void Formats::add(string const & name, string const & extension,
132 string const & prettyname, string const & shortcut)
134 FormatList::iterator it =
135 find_if(formatlist.begin(), formatlist.end(),
136 FormatNamesEqual(name));
137 if (it == formatlist.end())
138 formatlist.push_back(Format(name, extension, prettyname,
141 string viewer = it->viewer();
142 *it = Format(name, extension, prettyname, shortcut, viewer);
147 void Formats::erase(string const & name)
149 FormatList::iterator it =
150 find_if(formatlist.begin(), formatlist.end(),
151 FormatNamesEqual(name));
152 if (it != formatlist.end())
153 formatlist.erase(it);
159 std::sort(formatlist.begin(), formatlist.end());
163 void Formats::setViewer(string const & name, string const & command)
166 FormatList::iterator it =
167 find_if(formatlist.begin(), formatlist.end(),
168 FormatNamesEqual(name));
169 if (it != formatlist.end())
170 it->setViewer(command);
174 bool Formats::view(Buffer const & buffer, string const & filename,
175 string const & format_name) const
177 if (filename.empty())
180 Format const * format = getFormat(format_name);
181 if (format && format->viewer().empty() &&
182 format->isChildFormat())
183 format = getFormat(format->parentFormat());
184 if (!format || format->viewer().empty()) {
185 // I believe this is the wrong place to show alerts, it should be done by
186 // the caller (this should be "utility" code
187 Alert::error(_("Cannot view file"),
188 bformat(_("No information for viewing %1$s"),
189 prettyName(format_name)));
193 string command = format->viewer();
195 if (format_name == "dvi" &&
196 !lyxrc.view_dvi_paper_option.empty()) {
197 command += ' ' + lyxrc.view_dvi_paper_option;
198 string paper_size = buffer.params().paperSizeName();
199 if (paper_size == "letter")
201 command += ' ' + paper_size;
202 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
206 if (!contains(command, token_from))
207 command += ' ' + token_from;
209 command = subst(command, token_from,
210 QuoteName(OnlyFilename(filename)));
211 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
212 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
213 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
214 buffer.message(_("Executing command: ") + command);
216 Path p(OnlyPath(filename));
218 int const res = one.startscript(Systemcall::DontWait, command);
221 Alert::error(_("Cannot view file"),
222 bformat(_("An error occurred whilst running %1$s"),
223 command.substr(0, 50)));
230 string const Formats::prettyName(string const & name) const
232 Format const * format = getFormat(name);
234 return format->prettyname();
240 string const Formats::extension(string const & name) const
242 Format const * format = getFormat(name);
244 return format->extension();
254 Formats system_formats;