]> git.lyx.org Git - lyx.git/blob - src/Importer.cpp
adjust
[lyx.git] / src / Importer.cpp
1 /**
2  * \file Importer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Importer.h"
16 #include "Converter.h"
17 #include "Format.h"
18 #include "frontends/LyXView.h"
19 #include "FuncRequest.h"
20
21 #include "support/filetools.h"
22
23 #include "frontends/alert.h"
24
25 #include "gettext.h"
26 #include "BufferView.h"
27 #include "buffer_funcs.h"
28
29 using std::find;
30 using std::string;
31 using std::vector;
32
33
34 namespace lyx {
35
36 using support::bformat;
37 using support::changeExtension;
38 using support::FileName;
39 using support::makeDisplayPath;
40 using frontend::LyXView;
41
42
43 bool Importer::Import(LyXView * lv, FileName const & filename,
44                       string const & format, ErrorList & errorList)
45 {
46         docstring const displaypath = makeDisplayPath(filename.absFilename());
47         lv->message(bformat(_("Importing %1$s..."), displaypath));
48
49         FileName const lyxfile(changeExtension(filename.absFilename(), ".lyx"));
50
51         string loader_format;
52         vector<string> loaders = Loaders();
53         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
54                 for (vector<string>::const_iterator it = loaders.begin();
55                      it != loaders.end(); ++it) {
56                         if (theConverters().isReachable(format, *it)) {
57                                 string const tofile =
58                                         changeExtension(filename.absFilename(),
59                                                 formats.extension(*it));
60                                 if (!theConverters().convert(0, filename, FileName(tofile),
61                                                         filename, format, *it, errorList))
62                                         return false;
63                                 loader_format = *it;
64                                 break;
65                         }
66                 }
67                 if (loader_format.empty()) {
68                         frontend::Alert::error(_("Couldn't import file"),
69                                      bformat(_("No information for importing the format %1$s."),
70                                          formats.prettyName(format)));
71                         return false;
72                 }
73         } else {
74                 loader_format = format;
75         }
76
77
78         if (loader_format == "lyx") {
79                 Buffer * buf = lv->loadLyXFile(lyxfile);
80                 if (!buf) {
81                         // we are done
82                         lv->message(_("file not imported!"));
83                         return false;
84                 }
85                 updateLabels(*buf);
86                 lv->setBuffer(buf);
87                 lv->showErrorList("Parse");
88         } else {
89                 Buffer * const b = newFile(lyxfile.absFilename(), string(), true);
90                 if (b)
91                         lv->setBuffer(b);
92                 else
93                         return false;
94                 bool as_paragraphs = loader_format == "textparagraph";
95                 string filename2 = (loader_format == format) ? filename.absFilename()
96                         : changeExtension(filename.absFilename(),
97                                           formats.extension(loader_format));
98                 lv->view()->insertPlaintextFile(filename2, as_paragraphs);
99                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
100         }
101
102         // we are done
103         lv->message(_("imported."));
104         return true;
105 }
106
107
108 vector<Format const *> const Importer::GetImportableFormats()
109 {
110         vector<string> loaders = Loaders();
111         vector<Format const *> result =
112                 theConverters().getReachableTo(loaders[0], true);
113         for (vector<string>::const_iterator it = loaders.begin() + 1;
114              it != loaders.end(); ++it) {
115                 vector<Format const *> r =
116                         theConverters().getReachableTo(*it, false);
117                 result.insert(result.end(), r.begin(), r.end());
118         }
119         return result;
120 }
121
122
123 vector<string> const Importer::Loaders()
124 {
125         vector<string> v;
126         v.push_back("lyx");
127         v.push_back("text");
128         v.push_back("textparagraph");
129         return v;
130 }
131
132
133 } // namespace lyx