]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSendto.C
fix crash due to invalidated iterator
[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 #include "ControlSendto.h"
14
15 #include "buffer.h"
16 #include "converter.h"
17 #include "format.h"
18 #include "funcrequest.h"
19 #include "lyxrc.h"
20
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23
24 using std::vector;
25 using std::string;
26
27 namespace lyx {
28
29 using support::AddName;
30 using support::trim;
31
32 namespace frontend {
33
34
35 ControlSendto::ControlSendto(Dialog & parent)
36         : Dialog::Controller(parent)
37 {}
38
39
40 bool ControlSendto::initialiseParams(std::string const &)
41 {
42         format_ = 0;
43         command_ = lyxrc.custom_export_command;
44         return true;
45 }
46
47
48 void ControlSendto::dispatchParams()
49 {
50         if (command_.empty() || !format_ || format_->name().empty())
51                 return;
52
53         string const data = format_->name() + " " + command_;
54         kernel().dispatch(FuncRequest(getLfun(), data));
55 }
56
57
58 vector<Format const *> const ControlSendto::allFormats() const
59 {
60         // What formats can we output natively?
61         vector<string> exports;
62         exports.push_back("lyx");
63         exports.push_back("text");
64
65         Buffer const & buffer = kernel().buffer();
66
67         if (buffer.isLatex())
68                 exports.push_back("latex");
69         else if (buffer.isLinuxDoc())
70                 exports.push_back("linuxdoc");
71         else if (buffer.isDocBook())
72                 exports.push_back("docbook");
73         else if (buffer.isLiterate())
74                 exports.push_back("literate");
75
76         // Loop over these native formats and ascertain what formats we
77         // can convert to
78         vector<Format const *> to;
79
80         vector<string>::const_iterator ex_it  = exports.begin();
81         vector<string>::const_iterator ex_end = exports.end();
82         for (; ex_it != ex_end; ++ex_it) {
83                 // Start off with the native export format.
84                 // "formats" is LyX's list of recognised formats
85                 to.push_back(formats.getFormat(*ex_it));
86
87                 Formats::const_iterator fo_it  = formats.begin();
88                 Formats::const_iterator fo_end = formats.end();
89                 for (; fo_it != fo_end; ++fo_it) {
90                         // we need to hide the default graphic export formats
91                         // from the external menu, because we need them only
92                         // for the internal lyx-view and external latex run
93                         string const name = fo_it->name();
94                         if (name != "eps" && name != "xpm" && name != "png" &&
95                             converters.isReachable(*ex_it, name))
96                                 to.push_back(&(*fo_it));
97                 }
98         }
99
100         // Remove repeated formats.
101         std::sort(to.begin(), to.end());
102         to.erase(std::unique(to.begin(), to.end()), to.end());
103
104         return to;
105 }
106
107
108 void ControlSendto::setFormat(Format const * fmt)
109 {
110         format_ = fmt;
111 }
112
113
114 void ControlSendto::setCommand(string const & cmd)
115 {
116         command_ = trim(cmd);
117 }
118
119 } // namespace frontend
120 } // namespace lyx