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