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