]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
InsetHyperlink.cpp: fix a bug I introduced in r26218
[lyx.git] / src / Format.cpp
1 /**
2  * \file Format.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Format.h"
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "LyXRC.h"
17 #include "ServerSocket.h"
18
19 #include "frontends/alert.h" //to be removed?
20
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26 #include "support/Systemcall.h"
27
28 // FIXME: Q_WS_MACX is not available, it's in Qt
29 #ifdef USE_MACOSX_PACKAGING
30 #include "support/linkback/LinkBackProxy.h"
31 #endif
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38 namespace Alert = frontend::Alert;
39 namespace os = support::os;
40
41 namespace {
42
43 string const token_from_format("$$i");
44 string const token_path_format("$$p");
45 string const token_socket_format("$$a");
46
47
48 class FormatNamesEqual : public unary_function<Format, bool> {
49 public:
50         FormatNamesEqual(string const & name)
51                 : name_(name) {}
52         bool operator()(Format const & f) const
53         {
54                 return f.name() == name_;
55         }
56 private:
57         string name_;
58 };
59
60
61 class FormatExtensionsEqual : public unary_function<Format, bool> {
62 public:
63         FormatExtensionsEqual(string const & extension)
64                 : extension_(extension) {}
65         bool operator()(Format const & f) const
66         {
67                 return f.extension() == extension_;
68         }
69 private:
70         string extension_;
71 };
72
73 } //namespace anon
74
75 bool operator<(Format const & a, Format const & b)
76 {
77         // use the compare_ascii_no_case instead of compare_no_case,
78         // because in turkish, 'i' is not the lowercase version of 'I',
79         // and thus turkish locale breaks parsing of tags.
80
81         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
82 }
83
84
85 Format::Format(string const & n, string const & e, string const & p,
86                string const & s, string const & v, string const & ed,
87                int flags)
88         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
89           editor_(ed), flags_(flags)
90 {}
91
92
93 bool Format::dummy() const
94 {
95         return extension().empty();
96 }
97
98
99 bool Format::isChildFormat() const
100 {
101         if (name_.empty())
102                 return false;
103         return isdigit(name_[name_.length() - 1]);
104 }
105
106
107 string const Format::parentFormat() const
108 {
109         return name_.substr(0, name_.length() - 1);
110 }
111
112
113 // This method should return a reference, and throw an exception
114 // if the format named name cannot be found (Lgb)
115 Format const * Formats::getFormat(string const & name) const
116 {
117         FormatList::const_iterator cit =
118                 find_if(formatlist.begin(), formatlist.end(),
119                         FormatNamesEqual(name));
120         if (cit != formatlist.end())
121                 return &(*cit);
122         else
123                 return 0;
124 }
125
126
127 string Formats::getFormatFromFile(FileName const & filename) const
128 {
129         if (filename.empty())
130                 return string();
131
132         string const format = filename.guessFormatFromContents();
133         if (!format.empty())
134                 return format;
135
136         // try to find a format from the file extension.
137         string const ext = getExtension(filename.absFilename());
138         if (!ext.empty()) {
139                 // this is ambigous if two formats have the same extension,
140                 // but better than nothing
141                 Formats::const_iterator cit =
142                         find_if(formatlist.begin(), formatlist.end(),
143                                 FormatExtensionsEqual(ext));
144                 if (cit != formats.end()) {
145                         LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
146                                 << ext << " -> " << cit->name());
147                         return cit->name();
148                 }
149         }
150         return string();
151 }
152
153
154 static string fixCommand(string const & cmd, string const & ext,
155                   os::auto_open_mode mode)
156 {
157         // configure.py says we do not want a viewer/editor
158         if (cmd.empty())
159                 return cmd;
160
161         // Does the OS manage this format?
162         if (os::canAutoOpenFile(ext, mode))
163                 return "auto";
164
165         // if configure.py found nothing, clear the command
166         if (token(cmd, ' ', 0) == "auto")
167                 return string();
168
169         // use the command found by configure.py
170         return cmd;
171 }
172
173
174 void Formats::setAutoOpen()
175 {
176         FormatList::iterator fit = formatlist.begin();
177         FormatList::iterator const fend = formatlist.end();
178         for ( ; fit != fend ; ++fit) {
179                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
180                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
181         }
182 }
183
184
185 int Formats::getNumber(string const & name) const
186 {
187         FormatList::const_iterator cit =
188                 find_if(formatlist.begin(), formatlist.end(),
189                         FormatNamesEqual(name));
190         if (cit != formatlist.end())
191                 return distance(formatlist.begin(), cit);
192         else
193                 return -1;
194 }
195
196
197 void Formats::add(string const & name)
198 {
199         if (!getFormat(name))
200                 add(name, name, name, string(), string(), string(),
201                     Format::document);
202 }
203
204
205 void Formats::add(string const & name, string const & extension,
206                   string const & prettyname, string const & shortcut,
207                   string const & viewer, string const & editor,
208                   int flags)
209 {
210         FormatList::iterator it =
211                 find_if(formatlist.begin(), formatlist.end(),
212                         FormatNamesEqual(name));
213         if (it == formatlist.end())
214                 formatlist.push_back(Format(name, extension, prettyname,
215                                             shortcut, viewer, editor, flags));
216         else
217                 *it = Format(name, extension, prettyname, shortcut, viewer,
218                              editor, flags);
219 }
220
221
222 void Formats::erase(string const & name)
223 {
224         FormatList::iterator it =
225                 find_if(formatlist.begin(), formatlist.end(),
226                         FormatNamesEqual(name));
227         if (it != formatlist.end())
228                 formatlist.erase(it);
229 }
230
231
232 void Formats::sort()
233 {
234         std::sort(formatlist.begin(), formatlist.end());
235 }
236
237
238 void Formats::setViewer(string const & name, string const & command)
239 {
240         add(name);
241         FormatList::iterator it =
242                 find_if(formatlist.begin(), formatlist.end(),
243                         FormatNamesEqual(name));
244         if (it != formatlist.end())
245                 it->setViewer(command);
246 }
247
248
249 bool Formats::view(Buffer const & buffer, FileName const & filename,
250                    string const & format_name) const
251 {
252         if (filename.empty() || !filename.exists()) {
253                 Alert::error(_("Cannot view file"),
254                         bformat(_("File does not exist: %1$s"),
255                                 from_utf8(filename.absFilename())));
256                 return false;
257         }
258
259         Format const * format = getFormat(format_name);
260         if (format && format->viewer().empty() &&
261             format->isChildFormat())
262                 format = getFormat(format->parentFormat());
263         if (!format || format->viewer().empty()) {
264 // FIXME: I believe this is the wrong place to show alerts, it should be done
265 // by the caller (this should be "utility" code)
266                 Alert::error(_("Cannot view file"),
267                         bformat(_("No information for viewing %1$s"),
268                                 prettyName(format_name)));
269                 return false;
270         }
271         // viewer is 'auto'
272         if (format->viewer() == "auto") {
273                 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
274                         return true;
275                 else {
276                         Alert::error(_("Cannot view file"),
277                                 bformat(_("Auto-view file %1$s failed"),
278                                         from_utf8(filename.absFilename())));
279                         return false;
280                 }
281         }
282
283         string command = libScriptSearch(format->viewer());
284
285         if (format_name == "dvi" &&
286             !lyxrc.view_dvi_paper_option.empty()) {
287                 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
288                 if (!paper_size.empty()) {
289                         command += ' ' + lyxrc.view_dvi_paper_option;
290                         command += ' ' + paper_size;
291                         if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
292                             buffer.params().papersize != PAPER_CUSTOM)
293                                 command += 'r';
294                 }
295         }
296
297         if (!contains(command, token_from_format))
298                 command += ' ' + token_from_format;
299
300         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
301         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
302         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
303         LYXERR(Debug::FILES, "Executing command: " << command);
304         // FIXME UNICODE utf8 can be wrong for files
305         buffer.message(_("Executing command: ") + from_utf8(command));
306
307         Systemcall one;
308         int const res = one.startscript(Systemcall::DontWait, command);
309
310         if (res) {
311                 Alert::error(_("Cannot view file"),
312                              bformat(_("An error occurred whilst running %1$s"),
313                                makeDisplayPath(command, 50)));
314                 return false;
315         }
316         return true;
317 }
318
319
320 bool Formats::edit(Buffer const & buffer, FileName const & filename,
321                          string const & format_name) const
322 {
323         if (filename.empty() || !filename.exists()) {
324                 Alert::error(_("Cannot edit file"),
325                         bformat(_("File does not exist: %1$s"),
326                                 from_utf8(filename.absFilename())));
327                 return false;
328         }
329
330         // LinkBack files look like PDF, but have the .linkback extension
331         string const ext = getExtension(filename.absFilename());
332         if (format_name == "pdf" && ext == "linkback") {
333 #ifdef USE_MACOSX_PACKAGING
334                 return editLinkBackFile(filename.absFilename().c_str());
335 #else
336                 Alert::error(_("Cannot edit file"),
337                              _("LinkBack files can only be edited on Apple Mac OSX."));
338                 return false;
339 #endif // USE_MACOSX_PACKAGING
340         }
341
342         Format const * format = getFormat(format_name);
343         if (format && format->editor().empty() &&
344             format->isChildFormat())
345                 format = getFormat(format->parentFormat());
346         if (!format || format->editor().empty()) {
347 // FIXME: I believe this is the wrong place to show alerts, it should
348 // be done by the caller (this should be "utility" code)
349                 Alert::error(_("Cannot edit file"),
350                         bformat(_("No information for editing %1$s"),
351                                 prettyName(format_name)));
352                 return false;
353         }
354         
355         // editor is 'auto'
356         if (format->editor() == "auto") {
357                 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
358                         return true;
359                 else {
360                         Alert::error(_("Cannot edit file"),
361                                 bformat(_("Auto-edit file %1$s failed"),
362                                         from_utf8(filename.absFilename())));
363                         return false;
364                 }
365         }
366
367         string command = format->editor();
368
369         if (!contains(command, token_from_format))
370                 command += ' ' + token_from_format;
371
372         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
373         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
374         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
375         LYXERR(Debug::FILES, "Executing command: " << command);
376         // FIXME UNICODE utf8 can be wrong for files
377         buffer.message(_("Executing command: ") + from_utf8(command));
378
379         Systemcall one;
380         int const res = one.startscript(Systemcall::DontWait, command);
381
382         if (res) {
383                 Alert::error(_("Cannot edit file"),
384                              bformat(_("An error occurred whilst running %1$s"),
385                                makeDisplayPath(command, 50)));
386                 return false;
387         }
388         return true;
389 }
390
391
392 docstring const Formats::prettyName(string const & name) const
393 {
394         Format const * format = getFormat(name);
395         if (format)
396                 return from_utf8(format->prettyname());
397         else
398                 return from_utf8(name);
399 }
400
401
402 string const Formats::extension(string const & name) const
403 {
404         Format const * format = getFormat(name);
405         if (format)
406                 return format->extension();
407         else
408                 return name;
409 }
410
411
412
413
414 Formats formats;
415
416 Formats system_formats;
417
418
419 } // namespace lyx