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"
26 #include "support/lyxfunctional.h"
28 using lyx::support::bformat;
29 using lyx::support::compare_ascii_no_case;
30 using lyx::support::contains;
31 using lyx::support::OnlyFilename;
32 using lyx::support::OnlyPath;
33 using lyx::support::Path;
34 using lyx::support::QuoteName;
35 using lyx::support::subst;
36 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");
50 bool operator<(Format const & a, Format const & b)
52 // use the compare_ascii_no_case instead of compare_no_case,
53 // because in turkish, 'i' is not the lowercase version of 'I',
54 // and thus turkish locale breaks parsing of tags.
56 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
59 Format::Format(string const & n, string const & e, string const & p,
60 string const & s, string const & v): name_(n),
68 bool Format::dummy() const
70 return extension().empty();
74 bool Format::isChildFormat() const
78 return isdigit(name_[name_.length() - 1]);
82 string const Format::parentFormat() const
84 return name_.substr(0, name_.length() - 1);
88 // This method should return a reference, and throw an exception
89 // if the format named name cannot be found (Lgb)
90 Format const * Formats::getFormat(string const & name) const
92 FormatList::const_iterator cit =
93 find_if(formatlist.begin(), formatlist.end(),
94 lyx::compare_memfun(&Format::name, name));
95 if (cit != formatlist.end())
102 int Formats::getNumber(string const & name) const
104 FormatList::const_iterator cit =
105 find_if(formatlist.begin(), formatlist.end(),
106 lyx::compare_memfun(&Format::name, name));
107 if (cit != formatlist.end())
108 return cit - formatlist.begin();
114 void Formats::add(string const & name)
116 if (!getFormat(name))
117 add(name, name, name, string());
121 void Formats::add(string const & name, string const & extension,
122 string const & prettyname, string const & shortcut)
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.push_back(Format(name, extension, prettyname,
131 string viewer = it->viewer();
132 *it = Format(name, extension, prettyname, shortcut, viewer);
137 void Formats::erase(string const & name)
139 FormatList::iterator it =
140 find_if(formatlist.begin(), formatlist.end(),
141 lyx::compare_memfun(&Format::name, name));
142 if (it != formatlist.end())
143 formatlist.erase(it);
149 std::sort(formatlist.begin(), formatlist.end());
153 void Formats::setViewer(string const & name, string const & command)
156 FormatList::iterator it =
157 find_if(formatlist.begin(), formatlist.end(),
158 lyx::compare_memfun(&Format::name, name));
159 if (it != formatlist.end())
160 it->setViewer(command);
164 bool Formats::view(Buffer const & buffer, string const & filename,
165 string const & format_name) const
167 if (filename.empty())
170 Format const * format = getFormat(format_name);
171 if (format && format->viewer().empty() &&
172 format->isChildFormat())
173 format = getFormat(format->parentFormat());
174 if (!format || format->viewer().empty()) {
175 // I believe this is the wrong place to show alerts, it should be done by
176 // the caller (this should be "utility" code
177 Alert::error(_("Cannot view file"),
178 bformat(_("No information for viewing %1$s"),
179 prettyName(format_name)));
183 string command = format->viewer();
185 if (format_name == "dvi" &&
186 !lyxrc.view_dvi_paper_option.empty()) {
187 command += ' ' + lyxrc.view_dvi_paper_option;
188 string paper_size = buffer.params().paperSizeName();
189 if (paper_size == "letter")
191 command += ' ' + paper_size;
192 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
196 if (!contains(command, token_from))
197 command += ' ' + token_from;
199 command = subst(command, token_from,
200 QuoteName(OnlyFilename(filename)));
201 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
202 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
203 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
204 buffer.message(_("Executing command: ") + command);
206 Path p(OnlyPath(filename));
208 int const res = one.startscript(Systemcall::DontWait, command);
211 Alert::error(_("Cannot view file"),
212 bformat(_("An error occurred whilst running %1$s"),
213 command.substr(0, 50)));
220 string const Formats::prettyName(string const & name) const
222 Format const * format = getFormat(name);
224 return format->prettyname();
230 string const Formats::extension(string const & name) const
232 Format const * format = getFormat(name);
234 return format->extension();
244 Formats system_formats;