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