]> git.lyx.org Git - lyx.git/blob - src/importer.C
"Inter-word Space"
[lyx.git] / src / importer.C
1 /**
2  * \file exporter.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 "support/filetools.h"
22 #include "frontends/Alert.h"
23 #include "gettext.h"
24 #include "BufferView.h"
25
26 #include <algorithm>
27
28 using std::vector;
29 using std::find;
30
31
32 extern BufferList bufferlist;
33 extern void InsertAsciiFile(BufferView *, string const &, bool);
34
35
36 bool Importer::Import(LyXView * lv, string const & filename,
37                       string const & format)
38 {
39         string const displaypath = MakeDisplayPath(filename);
40         lv->message(bformat(_("Importing %1$s..."), displaypath));
41
42         string const lyxfile = ChangeExtension(filename, ".lyx");
43
44         string loader_format;
45         vector<string> loaders = Loaders();
46         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
47                 for (vector<string>::const_iterator it = loaders.begin();
48                      it != loaders.end(); ++it) {
49                         if (converters.isReachable(format, *it)) {
50                                 if (!converters.convert(0, filename, filename,
51                                                         format, *it))
52                                         return false;
53                                 loader_format = *it;
54                                 break;
55                         }
56                 }
57                 if (loader_format.empty()) {
58                         Alert::error(_("Couldn't import file"),
59                                      bformat(_("No information for importing the format %1$s."),
60                                          formats.prettyName(format)));
61                         return false;
62                 }
63         } else
64                 loader_format = format;
65
66
67         if (loader_format == "lyx") {
68                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
69                 if (buffer)
70                         lv->view()->buffer(buffer);
71         } else {
72                 lv->view()->buffer(bufferlist.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 }