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