]> git.lyx.org Git - lyx.git/blob - src/Importer.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / Importer.cpp
1 /**
2  * \file Importer.cpp
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 "callback.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 std::find;
31 using std::string;
32 using std::vector;
33
34
35 namespace lyx {
36
37 using support::bformat;
38 using support::changeExtension;
39 using support::FileName;
40 using support::makeDisplayPath;
41
42
43 bool Importer::Import(LyXView * lv, FileName const & filename,
44                       string const & format, ErrorList & errorList)
45 {
46         docstring const displaypath = makeDisplayPath(filename.absFilename());
47         lv->message(bformat(_("Importing %1$s..."), displaypath));
48
49         FileName const lyxfile(changeExtension(filename.absFilename(), ".lyx"));
50
51         string loader_format;
52         vector<string> loaders = Loaders();
53         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
54                 for (vector<string>::const_iterator it = loaders.begin();
55                      it != loaders.end(); ++it) {
56                         if (theConverters().isReachable(format, *it)) {
57                                 string const tofile =
58                                         changeExtension(filename.absFilename(),
59                                                 formats.extension(*it));
60                                 if (!theConverters().convert(0, filename, FileName(tofile),
61                                                         filename, format, *it, errorList))
62                                         return false;
63                                 loader_format = *it;
64                                 break;
65                         }
66                 }
67                 if (loader_format.empty()) {
68                         frontend::Alert::error(_("Couldn't import file"),
69                                      bformat(_("No information for importing the format %1$s."),
70                                          formats.prettyName(format)));
71                         return false;
72                 }
73         } else {
74                 loader_format = format;
75         }
76
77
78         if (loader_format == "lyx") {
79                 lv->loadLyXFile(lyxfile);
80         } else {
81                 Buffer * const b = newFile(lyxfile.absFilename(), string(), true);
82                 if (b)
83                         lv->setBuffer(b);
84                 else
85                         return false;
86                 bool as_paragraphs = loader_format == "textparagraph";
87                 string filename2 = (loader_format == format) ? filename.absFilename()
88                         : changeExtension(filename.absFilename(),
89                                           formats.extension(loader_format));
90                 insertPlaintextFile(lv->view(), filename2, as_paragraphs);
91                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
92         }
93
94         // we are done
95         lv->message(_("imported."));
96         return true;
97 }
98
99
100 vector<Format const *> const Importer::GetImportableFormats()
101 {
102         vector<string> loaders = Loaders();
103         vector<Format const *> result =
104                 theConverters().getReachableTo(loaders[0], true);
105         for (vector<string>::const_iterator it = loaders.begin() + 1;
106              it != loaders.end(); ++it) {
107                 vector<Format const *> r =
108                         theConverters().getReachableTo(*it, false);
109                 result.insert(result.end(), r.begin(), r.end());
110         }
111         return result;
112 }
113
114
115 vector<string> const Importer::Loaders()
116 {
117         vector<string> v;
118         v.push_back("lyx");
119         v.push_back("text");
120         v.push_back("textparagraph");
121         return v;
122 }
123
124
125 } // namespace lyx