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