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