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