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