2 * \file ControlSendto.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
14 #include "ControlSendto.h"
17 #include "converter.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/systemcall.h"
26 using lyx::support::AddName;
27 using lyx::support::ChangeExtension;
28 using lyx::support::contains;
29 using lyx::support::subst;
30 using lyx::support::Systemcall;
31 using lyx::support::trim;
36 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
37 : ControlDialogBD(lv, d),
39 command_(lyxrc.custom_export_command)
43 vector<Format const *> const ControlSendto::allFormats() const
45 // What formats can we output natively?
46 vector<string> exports;
47 exports.push_back("lyx");
48 exports.push_back("text");
50 #warning Can the doc ever be all/any of these at the same time? (Lgb)
51 // I think some if else if is in order.
52 if (buffer().isLatex())
53 exports.push_back("latex");
54 if (buffer().isLinuxDoc())
55 exports.push_back("linuxdoc");
56 if (buffer().isDocBook())
57 exports.push_back("docbook");
58 if (buffer().isLiterate())
59 exports.push_back("literate");
61 // Loop over these native formats and ascertain what formats we
63 vector<Format const *> to;
65 vector<string>::const_iterator ex_it = exports.begin();
66 vector<string>::const_iterator ex_end = exports.end();
67 for (; ex_it != ex_end; ++ex_it) {
68 // Start off with the native export format.
69 // "formats" is LyX's list of recognised formats
70 to.push_back(formats.getFormat(*ex_it));
72 Formats::const_iterator fo_it = formats.begin();
73 Formats::const_iterator fo_end = formats.end();
74 for (; fo_it != fo_end; ++fo_it) {
75 // we need to hide the default graphic export formats
76 // from the external menu, because we need them only
77 // for the internal lyx-view and external latex run
78 string const name = fo_it->name();
79 if (name != "eps" && name != "xpm" && name != "png" &&
80 converters.isReachable(*ex_it, name))
81 to.push_back(&(*fo_it));
85 // Remove repeated formats.
86 std::sort(to.begin(), to.end());
88 vector<Format const *>::iterator to_begin = to.begin();
89 vector<Format const *>::iterator to_end = to.end();
90 vector<Format const *>::iterator to_it =
91 std::unique(to_begin, to_end);
93 to.erase(to_it, to_end);
99 void ControlSendto::setFormat(Format const * fmt)
105 void ControlSendto::setCommand(string const & cmd)
107 command_ = trim(cmd);
111 void ControlSendto::apply()
113 if (!bufferIsAvailable())
118 if (command_.empty() || !format_)
121 // The name of the file created by the conversion process
124 // Output to filename
125 if (format_->name() == "lyx") {
126 filename = ChangeExtension(buffer()->getLatexName(false),
127 format_->extension());
128 if (!buffer()->temppath().empty())
129 filename = AddName(buffer()->temppath(), filename);
131 buffer()->writeFile(filename);
134 Exporter::Export(buffer(), format_->name(), true, filename);
137 // Substitute $$FName for filename
138 string command = command_;
139 if (!contains(command, "$$FName"))
140 command = "( " + command + " ) < $$FName";
141 command = subst(command, "$$FName", filename);
143 // Execute the command in the background
145 call.startscript(Systemcall::DontWait, command);