]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSendto.C
Finish ControlSendto.
[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 #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 "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                         if (converters.isReachable(*ex_it, fo_it->name())) {
74                                 to.push_back(fo_it);
75                         }
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_ = strip(frontStrip(cmd));
102 }
103
104
105 void ControlSendto::apply()
106 {
107         if (!lv_.view()->available())
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(lv_.buffer()->getLatexName(false),
121                                            format_->extension());
122                 if (!lv_.buffer()->tmppath.empty())
123                         filename = AddName(lv_.buffer()->tmppath, filename);
124
125                 lv_.buffer()->writeFile(filename, true);
126
127         } else {
128                 Exporter::Export(lv_.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 }
141