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