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