]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
Add a buffer_path arg to InsetGraphicsMailer's params2string, string2params.
[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, *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().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
54 }
55
56
57 void ControlExternal::setParams(InsetExternal::Params const & p)
58 {
59         Assert(params_.get());
60         *params_ = p;
61 }
62
63
64 InsetExternal::Params const & ControlExternal::params() const
65 {
66         Assert(params_.get());
67         return *params_;
68 }
69
70
71 void ControlExternal::editExternal()
72 {
73         Assert(params_.get());
74
75         dialog().view().apply();
76         string const lfun = InsetExternalMailer::params2string(params());
77         kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
78 }
79
80
81 vector<string> const ControlExternal::getTemplates() const
82 {
83         vector<string> result;
84
85         ExternalTemplateManager::Templates::const_iterator i1, i2;
86         i1 = ExternalTemplateManager::get().getTemplates().begin();
87         i2 = ExternalTemplateManager::get().getTemplates().end();
88
89         for (; i1 != i2; ++i1) {
90                 result.push_back(i1->second.lyxName);
91         }
92         return result;
93 }
94
95
96 int ControlExternal::getTemplateNumber(string const & name) const
97 {
98         ExternalTemplateManager::Templates::const_iterator i1, i2;
99         i1 = ExternalTemplateManager::get().getTemplates().begin();
100         i2 = ExternalTemplateManager::get().getTemplates().end();
101         for (int i = 0; i1 != i2; ++i1, ++i) {
102                 if (i1->second.lyxName == name)
103                         return i;
104         }
105
106         // we can get here if a LyX document has a template not installed
107         // on this machine.
108         return -1;
109 }
110
111
112 ExternalTemplate ControlExternal::getTemplate(int i) const
113 {
114         ExternalTemplateManager::Templates::const_iterator i1
115                 = ExternalTemplateManager::get().getTemplates().begin();
116
117         std::advance(i1, i);
118
119         return i1->second;
120 }
121
122
123 namespace {
124
125 ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params)
126 {
127         ExternalTemplateManager & etm = ExternalTemplateManager::get();
128         ExternalTemplate const & templ = etm.getTemplateByName(params.templatename);
129         if (templ.lyxName.empty())
130                 return 0;
131         return &templ;
132 }
133
134 } // namespace anon
135
136
137 string const ControlExternal::Browse(string const & input) const
138 {
139         string const title =  _("Select external file");
140
141         string const bufpath = kernel().buffer()->filePath();
142
143         /// Determine the template file extension
144         string pattern = "*";
145         ExternalTemplate const * const et_ptr = getTemplatePtr(params());
146         if (et_ptr)
147                 pattern = et_ptr->fileRegExp;
148
149         // FIXME: a temporary hack until the FileDialog interface is updated
150         pattern += '|';
151
152         std::pair<string, string> dir1(N_("Documents|#o#O"),
153                                   string(lyxrc.document_path));
154
155         return browseRelFile(input, bufpath, title, pattern, false, dir1);
156 }