]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSendto.C
Alfredo's patch
[lyx.git] / src / frontends / controllers / ControlSendto.C
1 /**
2  * \file ControlSendto.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "ControlSendto.h"
15 #include "ViewBase.h"
16 #include "buffer.h"
17 #include "converter.h"
18 #include "format.h"
19 #include "exporter.h"
20 #include "gettext.h"
21 #include "lyxrc.h"
22
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
26
27 using std::vector;
28
29
30 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
31         : ControlDialogBD(lv, d),
32           format_(0),
33           command_(lyxrc.custom_export_command)
34 {}
35
36
37 vector<Format const *> const ControlSendto::allFormats() const
38 {
39         // What formats can we output natively?
40         vector<string> exports;
41         exports.push_back("lyx");
42         exports.push_back("text");
43
44         if (buffer()->isLatex())
45                 exports.push_back("latex");
46         if (buffer()->isLinuxDoc())
47                 exports.push_back("linuxdoc");
48         if (buffer()->isDocBook())
49                 exports.push_back("docbook");
50         if (buffer()->isLiterate())
51                 exports.push_back("literate");
52
53         // Loop over these native formats and ascertain what formats we
54         // can convert to
55         vector<Format const *> to;
56
57         vector<string>::const_iterator ex_it  = exports.begin();
58         vector<string>::const_iterator ex_end = exports.end();
59         for (; ex_it != ex_end; ++ex_it) {
60                 // Start off with the native export format.
61                 // "formats" is LyX's list of recognised formats
62                 to.push_back(formats.getFormat(*ex_it));
63
64                 Formats::const_iterator fo_it  = formats.begin();
65                 Formats::const_iterator fo_end = formats.end();
66                 for (; fo_it != fo_end; ++fo_it) {
67                         // we need to hide the default graphic export formats
68                         // from the external menu, because we need them only
69                         // for the internal lyx-view and external latex run
70                         string const name = fo_it->name();
71                         if (name != "eps" && name != "xpm" && name != "png" &&
72                             converters.isReachable(*ex_it, name))
73                                 to.push_back(&(*fo_it));
74                 }
75         }
76
77         // Remove repeated formats.
78         std::sort(to.begin(), to.end());
79
80         vector<Format const *>::iterator to_begin = to.begin();
81         vector<Format const *>::iterator to_end   = to.end();
82         vector<Format const *>::iterator to_it =
83                 std::unique(to_begin, to_end);
84         if (to_it != to_end)
85                 to.erase(to_it, to_end);
86
87         return to;
88 }
89
90
91 void ControlSendto::setFormat(Format const * fmt)
92 {
93         format_ = fmt;
94 }
95
96
97 void ControlSendto::setCommand(string const & cmd)
98 {
99         command_ = trim(cmd);
100 }
101
102
103 void ControlSendto::apply()
104 {
105         if (!bufferIsAvailable())
106                 return;
107
108         view().apply();
109
110         if (command_.empty() || !format_)
111                 return;
112
113         // The name of the file created by the conversion process
114         string filename;
115
116         // Output to filename
117         if (format_->name() == "lyx") {
118                 filename = ChangeExtension(buffer()->getLatexName(false),
119                                            format_->extension());
120                 if (!buffer()->tmppath.empty())
121                         filename = AddName(buffer()->tmppath, filename);
122
123                 buffer()->writeFile(filename);
124
125         } else {
126                 Exporter::Export(buffer(), format_->name(), true, filename);
127         }
128
129         // Substitute $$FName for filename
130         string command = command_;
131         if (!contains(command, "$$FName"))
132                 command = "( " + command + " ) < $$FName";
133         command = subst(command, "$$FName", filename);
134
135         // Execute the command in the background
136         Systemcall call;
137         call.startscript(Systemcall::DontWait, command);
138 }