]> git.lyx.org Git - lyx.git/blob - src/importer.C
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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 "bufferlist.h"
23 #include "support/filetools.h"
24 #include "frontends/Alert.h"
25 #include "gettext.h"
26 #include "BufferView.h"
27 #include "buffer_funcs.h"
28
29 using lyx::docstring;
30 using lyx::support::bformat;
31 using lyx::support::changeExtension;
32 using lyx::support::makeDisplayPath;
33
34 using std::find;
35 using std::string;
36 using std::vector;
37
38
39 extern BufferList bufferlist;
40
41
42 bool Importer::Import(LyXView * lv, string const & filename,
43                       string const & format, ErrorList & errorList)
44 {
45         docstring const displaypath = makeDisplayPath(filename);
46         lv->message(bformat(_("Importing %1$s..."), displaypath));
47
48         string const lyxfile = changeExtension(filename, ".lyx");
49
50         string loader_format;
51         vector<string> loaders = Loaders();
52         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
53                 for (vector<string>::const_iterator it = loaders.begin();
54                      it != loaders.end(); ++it) {
55                         if (converters.isReachable(format, *it)) {
56                                 if (!converters.convert(0, filename, filename,
57                                                         format, *it, errorList))
58                                         return false;
59                                 loader_format = *it;
60                                 break;
61                         }
62                 }
63                 if (loader_format.empty()) {
64                         Alert::error(_("Couldn't import file"),
65                                      bformat(_("No information for importing the format %1$s."),
66                                          formats.prettyName(format)));
67                         return false;
68                 }
69         } else
70                 loader_format = format;
71
72
73         if (loader_format == "lyx") {
74                 lv->loadLyXFile(lyxfile);
75         } else {
76                 Buffer * const b = newFile(lyxfile, string(), true);
77                 if (b)
78                         lv->setBuffer(b);
79                 else
80                         return false;
81                 bool as_paragraphs = loader_format == "textparagraph";
82                 string filename2 = (loader_format == format) ? filename
83                         : changeExtension(filename,
84                                           formats.extension(loader_format));
85                 insertAsciiFile(lv->view(), filename2, as_paragraphs);
86                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
87         }
88
89         // we are done
90         lv->message(_("imported."));
91         return true;
92 }
93
94
95 vector<Format const *> const Importer::GetImportableFormats()
96 {
97         vector<string> loaders = Loaders();
98         vector<Format const *> result =
99                 converters.getReachableTo(loaders[0], true);
100         for (vector<string>::const_iterator it = loaders.begin() + 1;
101              it != loaders.end(); ++it) {
102                 vector<Format const *> r =
103                         converters.getReachableTo(*it, false);
104                 result.insert(result.end(), r.begin(), r.end());
105         }
106         return result;
107 }
108
109
110 vector<string> const Importer::Loaders()
111 {
112         vector<string> v;
113         v.push_back("lyx");
114         v.push_back("text");
115         v.push_back("textparagraph");
116         return v;
117 }