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;
37 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
38 : ControlDialogBD(lv, d),
40 command_(lyxrc.custom_export_command)
44 vector<Format const *> const ControlSendto::allFormats() const
46 // What formats can we output natively?
47 vector<string> exports;
48 exports.push_back("lyx");
49 exports.push_back("text");
51 #warning Can the doc ever be all/any of these at the same time? (Lgb)
52 // I think some if else if is in order.
53 if (buffer().isLatex())
54 exports.push_back("latex");
55 if (buffer().isLinuxDoc())
56 exports.push_back("linuxdoc");
57 if (buffer().isDocBook())
58 exports.push_back("docbook");
59 if (buffer().isLiterate())
60 exports.push_back("literate");
62 // Loop over these native formats and ascertain what formats we
64 vector<Format const *> to;
66 vector<string>::const_iterator ex_it = exports.begin();
67 vector<string>::const_iterator ex_end = exports.end();
68 for (; ex_it != ex_end; ++ex_it) {
69 // Start off with the native export format.
70 // "formats" is LyX's list of recognised formats
71 to.push_back(formats.getFormat(*ex_it));
73 Formats::const_iterator fo_it = formats.begin();
74 Formats::const_iterator fo_end = formats.end();
75 for (; fo_it != fo_end; ++fo_it) {
76 // we need to hide the default graphic export formats
77 // from the external menu, because we need them only
78 // for the internal lyx-view and external latex run
79 string const name = fo_it->name();
80 if (name != "eps" && name != "xpm" && name != "png" &&
81 converters.isReachable(*ex_it, name))
82 to.push_back(&(*fo_it));
86 // Remove repeated formats.
87 std::sort(to.begin(), to.end());
88 to.erase(std::unique(to.begin(), to.end()), to.end());
94 void ControlSendto::setFormat(Format const * fmt)
100 void ControlSendto::setCommand(string const & cmd)
102 command_ = trim(cmd);
106 void ControlSendto::apply()
108 if (!bufferIsAvailable())
113 if (command_.empty() || !format_)
116 // The name of the file created by the conversion process
119 // Output to filename
120 if (format_->name() == "lyx") {
121 filename = ChangeExtension(buffer()->getLatexName(false),
122 format_->extension());
123 if (!buffer()->temppath().empty())
124 filename = AddName(buffer()->temppath(), filename);
126 buffer()->writeFile(filename);
129 Exporter::Export(buffer(), format_->name(), true, filename);
132 // Substitute $$FName for filename
133 string command = command_;
134 if (!contains(command, "$$FName"))
135 command = "( " + command + " ) < $$FName";
136 command = subst(command, "$$FName", filename);
138 // Execute the command in the background
140 call.startscript(Systemcall::DontWait, command);