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