]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
Use Buffer const reference in most placees possible.
[lyx.git] / src / frontends / controllers / ControlExternal.C
1 /**
2  * \file ControlExternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "ControlExternal.h"
16 #include "buffer.h"
17 #include "funcrequest.h"
18 #include "gettext.h"
19 #include "helper_funcs.h"
20 #include "lyxrc.h"
21
22 #include "insets/ExternalTemplate.h"
23
24 #include "support/LAssert.h"
25 #include <vector>
26
27 using namespace lyx::support;
28 using std::vector;
29
30
31 ControlExternal::ControlExternal(Dialog & parent)
32         : Dialog::Controller(parent)
33 {}
34
35
36 bool ControlExternal::initialiseParams(string const & data)
37 {
38         params_.reset(new InsetExternal::Params);
39         InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
40         return true;
41 }
42
43
44 void ControlExternal::clearParams()
45 {
46         params_.reset();
47 }
48
49
50 void ControlExternal::dispatchParams()
51 {
52         string const lfun = InsetExternalMailer::params2string(params(),
53                                                                kernel().buffer());
54         kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
55 }
56
57
58 void ControlExternal::setParams(InsetExternal::Params const & p)
59 {
60         Assert(params_.get());
61         *params_ = p;
62 }
63
64
65 InsetExternal::Params const & ControlExternal::params() const
66 {
67         Assert(params_.get());
68         return *params_;
69 }
70
71
72 void ControlExternal::editExternal()
73 {
74         Assert(params_.get());
75
76         dialog().view().apply();
77         string const lfun =
78                 InsetExternalMailer::params2string(params(), kernel().buffer());
79         kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
80 }
81
82
83 vector<string> const ControlExternal::getTemplates() const
84 {
85         vector<string> result;
86
87         ExternalTemplateManager::Templates::const_iterator i1, i2;
88         i1 = ExternalTemplateManager::get().getTemplates().begin();
89         i2 = ExternalTemplateManager::get().getTemplates().end();
90
91         for (; i1 != i2; ++i1) {
92                 result.push_back(i1->second.lyxName);
93         }
94         return result;
95 }
96
97
98 int ControlExternal::getTemplateNumber(string const & name) const
99 {
100         ExternalTemplateManager::Templates::const_iterator i1, i2;
101         i1 = ExternalTemplateManager::get().getTemplates().begin();
102         i2 = ExternalTemplateManager::get().getTemplates().end();
103         for (int i = 0; i1 != i2; ++i1, ++i) {
104                 if (i1->second.lyxName == name)
105                         return i;
106         }
107
108         // we can get here if a LyX document has a template not installed
109         // on this machine.
110         return -1;
111 }
112
113
114 ExternalTemplate ControlExternal::getTemplate(int i) const
115 {
116         ExternalTemplateManager::Templates::const_iterator i1
117                 = ExternalTemplateManager::get().getTemplates().begin();
118
119         std::advance(i1, i);
120
121         return i1->second;
122 }
123
124
125 namespace {
126
127 ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params)
128 {
129         ExternalTemplateManager & etm = ExternalTemplateManager::get();
130         ExternalTemplate const & templ = etm.getTemplateByName(params.templatename);
131         if (templ.lyxName.empty())
132                 return 0;
133         return &templ;
134 }
135
136 } // namespace anon
137
138
139 string const ControlExternal::Browse(string const & input) const
140 {
141         string const title =  _("Select external file");
142
143         string const bufpath = kernel().bufferFilepath();
144
145         /// Determine the template file extension
146         string pattern = "*";
147         ExternalTemplate const * const et_ptr = getTemplatePtr(params());
148         if (et_ptr)
149                 pattern = et_ptr->fileRegExp;
150
151         // FIXME: a temporary hack until the FileDialog interface is updated
152         pattern += '|';
153
154         std::pair<string, string> dir1(N_("Documents|#o#O"),
155                                   string(lyxrc.document_path));
156
157         return browseRelFile(input, bufpath, title, pattern, false, dir1);
158 }