]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSendto.C
Switch from SigC signals to boost::signals
[lyx.git] / src / frontends / controllers / ControlSendto.C
1 /**
2  * \file ControlSendto.C
3  * Copyright 2002 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Angus Leeming, a.leeming@.ac.uk
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "ViewBase.h"
16 #include "ButtonControllerBase.h"
17 #include "ControlSendto.h"
18 #include "Dialogs.h"
19 #include "frontends/LyXView.h"
20 #include "BufferView.h"
21 #include "buffer.h"
22 #include "converter.h"
23 #include "exporter.h"
24 #include "gettext.h"
25 #include "lyxrc.h"
26
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29 #include "support/systemcall.h"
30
31 #include <boost/bind.hpp>
32
33 #include <fstream>
34
35 using std::vector;
36
37 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
38         : ControlDialogBD(lv, d),
39           format_(0),
40           command_(lyxrc.custom_export_command)
41 {
42         d_.showSendto = boost::bind(&ControlSendto::show, this);
43 }
44
45
46 vector<Format const *> const ControlSendto::allFormats() const
47 {
48         // What formats can we output natively?
49         vector<string> exports;
50         exports.push_back("lyx");
51         exports.push_back("text");
52
53         if (lv_.buffer()->isLatex())
54                 exports.push_back("latex");
55         if (lv_.buffer()->isLinuxDoc())
56                 exports.push_back("linuxdoc");
57         if (lv_.buffer()->isDocBook())
58                 exports.push_back("docbook");
59         if (lv_.buffer()->isLiterate())
60                 exports.push_back("literate");
61
62         // Loop over these native formats and ascertain what formats we
63         // can convert to
64         vector<Format const *> to;
65
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));
72
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));
83                 }
84         }
85
86         // Remove repeated formats.
87         std::sort(to.begin(), to.end());
88
89         vector<Format const *>::iterator to_begin = to.begin();
90         vector<Format const *>::iterator to_end   = to.end();
91         vector<Format const *>::iterator to_it =
92                 std::unique(to_begin, to_end);
93         if (to_it != to_end)
94                 to.erase(to_it, to_end);
95
96         return to;
97 }
98
99
100 void ControlSendto::setFormat(Format const * fmt)
101 {
102         format_ = fmt;
103 }
104
105
106 void ControlSendto::setCommand(string const & cmd)
107 {
108         command_ = strip(frontStrip(cmd));
109 }
110
111
112 void ControlSendto::apply()
113 {
114         if (!lv_.view()->available())
115                 return;
116
117         view().apply();
118
119         if (command_.empty() || !format_)
120                 return;
121
122         // The name of the file created by the conversion process
123         string filename;
124
125         // Output to filename
126         if (format_->name() == "lyx") {
127                 filename = ChangeExtension(lv_.buffer()->getLatexName(false),
128                                            format_->extension());
129                 if (!lv_.buffer()->tmppath.empty())
130                         filename = AddName(lv_.buffer()->tmppath, filename);
131
132                 lv_.buffer()->writeFile(filename, true);
133
134         } else {
135                 Exporter::Export(lv_.buffer(), format_->name(), true, filename);
136         }
137
138         // Substitute $$FName for filename
139         string command = command_;
140         if (!contains(command, "$$FName"))
141                 command = "( " + command + " ) < $$FName";
142         command = subst(command, "$$FName", filename);
143
144         // Execute the command in the background
145         Systemcall call;
146         call.startscript(Systemcall::DontWait, command);
147 }