]> git.lyx.org Git - lyx.git/blob - src/importer.C
b2d6f12156d10f693f6fb8762781344ea196b7f1
[lyx.git] / src / importer.C
1 /**
2  * \file importer.C
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 #include "lyx_cb.h"
21
22 #include "bufferlist.h"
23 #include "support/filetools.h"
24 #include "frontends/Alert.h"
25 #include "gettext.h"
26 #include "BufferView.h"
27 #include "buffer_funcs.h"
28
29 using lyx::support::bformat;
30 using lyx::support::changeExtension;
31 using lyx::support::makeDisplayPath;
32
33 using std::find;
34 using std::string;
35 using std::vector;
36
37
38 extern BufferList bufferlist;
39
40
41 bool Importer::Import(LyXView * lv, string const & filename,
42                       string const & format)
43 {
44         string const displaypath = makeDisplayPath(filename);
45         lv->message(bformat(_("Importing %1$s..."), displaypath));
46
47         string const lyxfile = changeExtension(filename, ".lyx");
48
49         string loader_format;
50         vector<string> loaders = Loaders();
51         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
52                 for (vector<string>::const_iterator it = loaders.begin();
53                      it != loaders.end(); ++it) {
54                         if (converters.isReachable(format, *it)) {
55                                 if (!converters.convert(0, filename, filename,
56                                                         format, *it))
57                                         return false;
58                                 loader_format = *it;
59                                 break;
60                         }
61                 }
62                 if (loader_format.empty()) {
63                         Alert::error(_("Couldn't import file"),
64                                      bformat(_("No information for importing the format %1$s."),
65                                          formats.prettyName(format)));
66                         return false;
67                 }
68         } else
69                 loader_format = format;
70
71
72         if (loader_format == "lyx") {
73                 lv->loadLyXFile(lyxfile);
74         } else {
75                 lv->setBuffer(newFile(lyxfile, string(), true));
76                 bool as_paragraphs = loader_format == "textparagraph";
77                 string filename2 = (loader_format == format) ? filename
78                         : changeExtension(filename,
79                                           formats.extension(loader_format));
80                 insertAsciiFile(lv->view(), filename2, as_paragraphs);
81                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
82         }
83
84         // we are done
85         lv->message(_("imported."));
86         return true;
87 }
88
89
90 vector<Format const *> const Importer::GetImportableFormats()
91 {
92         vector<string> loaders = Loaders();
93         vector<Format const *> result =
94                 converters.getReachableTo(loaders[0], true);
95         for (vector<string>::const_iterator it = loaders.begin() + 1;
96              it != loaders.end(); ++it) {
97                 vector<Format const *> r =
98                         converters.getReachableTo(*it, false);
99                 result.insert(result.end(), r.begin(), r.end());
100         }
101         return result;
102 }
103
104
105 vector<string> const Importer::Loaders()
106 {
107         vector<string> v;
108         v.push_back("lyx");
109         v.push_back("text");
110         v.push_back("textparagraph");
111         return v;
112 }