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