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