]> git.lyx.org Git - lyx.git/blob - src/importer.C
Rename namespace Alert to lyx::frontend::Alert.
[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 using lyx::docstring;
31 using lyx::support::bformat;
32 using lyx::support::changeExtension;
33 using lyx::support::makeDisplayPath;
34
35 using std::find;
36 using std::string;
37 using std::vector;
38
39
40 bool Importer::Import(LyXView * lv, string const & filename,
41                       string const & format, ErrorList & errorList)
42 {
43         docstring 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, errorList))
56                                         return false;
57                                 loader_format = *it;
58                                 break;
59                         }
60                 }
61                 if (loader_format.empty()) {
62                         lyx::frontend::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->loadLyXFile(lyxfile);
73         } else {
74                 Buffer * const b = newFile(lyxfile, string(), true);
75                 if (b)
76                         lv->setBuffer(b);
77                 else
78                         return false;
79                 bool as_paragraphs = loader_format == "textparagraph";
80                 string filename2 = (loader_format == format) ? filename
81                         : changeExtension(filename,
82                                           formats.extension(loader_format));
83                 insertAsciiFile(lv->view(), filename2, as_paragraphs);
84                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
85         }
86
87         // we are done
88         lv->message(_("imported."));
89         return true;
90 }
91
92
93 vector<Format const *> const Importer::GetImportableFormats()
94 {
95         vector<string> loaders = Loaders();
96         vector<Format const *> result =
97                 converters.getReachableTo(loaders[0], true);
98         for (vector<string>::const_iterator it = loaders.begin() + 1;
99              it != loaders.end(); ++it) {
100                 vector<Format const *> r =
101                         converters.getReachableTo(*it, false);
102                 result.insert(result.end(), r.begin(), r.end());
103         }
104         return result;
105 }
106
107
108 vector<string> const Importer::Loaders()
109 {
110         vector<string> v;
111         v.push_back("lyx");
112         v.push_back("text");
113         v.push_back("textparagraph");
114         return v;
115 }