]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlEmbeddedFiles.cpp
half of another one
[lyx.git] / src / frontends / controllers / ControlEmbeddedFiles.cpp
1 /**
2  * \file ControlEmbeddedFiles.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlEmbeddedFiles.h"
14
15 #include "Buffer.h"
16
17 #include "FuncRequest.h"
18 #include "gettext.h"
19 #include "debug.h"
20 #include "Format.h"
21 #include "LyXRC.h"
22
23 #include "frontend_helpers.h"
24 #include "frontends/LyXView.h"
25
26 #include "support/FileFilterList.h"
27 #include "support/convert.h"
28
29 using std::string;
30
31 namespace lyx {
32
33 using support::FileFilterList;
34
35 namespace frontend {
36
37 ControlEmbeddedFiles::ControlEmbeddedFiles(Dialog & parent)
38         : Controller(parent)
39 {}
40
41
42 EmbeddedFiles & ControlEmbeddedFiles::embeddedFiles()
43 {
44         return buffer().embeddedFiles();
45 }
46
47
48 bool ControlEmbeddedFiles::initialiseParams(string const &)
49 {
50         return true;
51 }
52
53
54 void ControlEmbeddedFiles::updateEmbeddedFiles()
55 {
56         // copy buffer embeddedFiles to a local copy
57         buffer().embeddedFiles().update();
58         buffer().embeddingChanged();
59 }
60
61
62 void ControlEmbeddedFiles::dispatchMessage(string const & msg)
63 {
64         // FIXME: the right thing to do? QT guys?
65         // lyx view will only be updated if we do something to the main window. :-)
66         dispatch(FuncRequest(LFUN_MESSAGE, msg));
67 }
68
69
70 bool ControlEmbeddedFiles::isReadonly()
71 {
72         return buffer().isReadonly();
73 }
74
75
76 void ControlEmbeddedFiles::setEmbedding(bool enable)
77 {
78         if (embeddedFiles().enabled() == enable)
79                 return;
80         embeddedFiles().enable(enable);
81         buffer().markDirty();
82         if (enable)
83                 dispatchMessage("Stop saving in bundled format.");
84         else
85                 dispatchMessage("Save in bundled format.");
86 }
87
88
89 void ControlEmbeddedFiles::goTo(EmbeddedFile const & item, int idx)
90 {
91         BOOST_ASSERT(idx < item.refCount());
92         item.saveBookmark(&buffer(), idx);
93         lyxview().dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
94 }
95
96
97 void ControlEmbeddedFiles::view(EmbeddedFile const & item)
98 {
99         formats.view(buffer(), item, formats.getFormatFromFile(item));
100 }
101
102
103 void ControlEmbeddedFiles::setEmbed(EmbeddedFile & item, bool embed, bool update)
104 {
105         if (item.embedded() == embed)
106                 return;
107         item.setEmbed(embed);
108         if (update) {
109                 if (embed)
110                         item.updateFromExternalFile(&buffer());
111                 else
112                         item.extract(&buffer());
113                 item.updateInsets(&buffer());
114                 // FIXME: unless we record the type of file item, we will
115                 // need to update all possible dialogs (bibtex etc).
116                 updateDialog("graphics");
117         }
118         if (embed)
119                 dispatchMessage("Embed file " + item.outputFilename(buffer().filePath()));
120         else
121                 dispatchMessage("Stop embedding file " + item.outputFilename(buffer().filePath()));
122         buffer().markDirty();
123 }
124
125
126 bool ControlEmbeddedFiles::browseAndAddFile()
127 {
128         std::pair<docstring, docstring> dir1(_("Documents|#o#O"),
129                                   from_utf8(lyxrc.document_path));
130         FileFilterList const filter(_("All file (*.*)"));
131         docstring const file = browseRelFile(docstring(), from_utf8(bufferFilepath()),
132                              _("Select a file to embed"),
133                              filter, false, dir1);
134         if (!file.empty()) {
135                 EmbeddedFile & ef = embeddedFiles().registerFile(to_utf8(file), true);
136                 if (embeddedFiles().enabled())
137                         ef.updateFromExternalFile(&buffer());
138                 buffer().markDirty();
139                 dispatchMessage("Add an embedded file" + to_utf8(file));
140                 return true;
141         }
142         return false;
143 }
144
145
146 bool ControlEmbeddedFiles::extract(EmbeddedFile const & item)
147 {
148         if (item.embedded())
149                 return item.extract(&buffer());
150         else
151                 return false;
152 }
153
154
155 bool ControlEmbeddedFiles::update(EmbeddedFile const & item)
156 {
157         if (item.embedded())
158                 return item.updateFromExternalFile(&buffer());
159         else
160                 return false;
161 }
162
163 } // namespace frontend
164 } // namespace lyx