]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSendto.C
Small clean-up.
[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 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;
32
33 using std::vector;
34
35
36 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
37         : ControlDialogBD(lv, d),
38           format_(0),
39           command_(lyxrc.custom_export_command)
40 {}
41
42
43 vector<Format const *> const ControlSendto::allFormats() const
44 {
45         // What formats can we output natively?
46         vector<string> exports;
47         exports.push_back("lyx");
48         exports.push_back("text");
49
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");
60
61         // Loop over these native formats and ascertain what formats we
62         // can convert to
63         vector<Format const *> to;
64
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));
71
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));
82                 }
83         }
84
85         // Remove repeated formats.
86         std::sort(to.begin(), to.end());
87
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);
92         if (to_it != to_end)
93                 to.erase(to_it, to_end);
94
95         return to;
96 }
97
98
99 void ControlSendto::setFormat(Format const * fmt)
100 {
101         format_ = fmt;
102 }
103
104
105 void ControlSendto::setCommand(string const & cmd)
106 {
107         command_ = trim(cmd);
108 }
109
110
111 void ControlSendto::apply()
112 {
113         if (!bufferIsAvailable())
114                 return;
115
116         view().apply();
117
118         if (command_.empty() || !format_)
119                 return;
120
121         // The name of the file created by the conversion process
122         string filename;
123
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);
130
131                 buffer()->writeFile(filename);
132
133         } else {
134                 Exporter::Export(buffer(), format_->name(), true, filename);
135         }
136
137         // Substitute $$FName for filename
138         string command = command_;
139         if (!contains(command, "$$FName"))
140                 command = "( " + command + " ) < $$FName";
141         command = subst(command, "$$FName", filename);
142
143         // Execute the command in the background
144         Systemcall call;
145         call.startscript(Systemcall::DontWait, command);
146 }