]> git.lyx.org Git - lyx.git/blob - src/importer.C
Oops...
[lyx.git] / src / importer.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "importer.h"
14 #include "converter.h"
15 #include "format.h"
16 #include "frontends/LyXView.h"
17 #include "funcrequest.h"
18
19 #include "bufferlist.h"
20 #include "support/filetools.h"
21 #include "frontends/Alert.h"
22 #include "gettext.h"
23 #include "BufferView.h"
24
25 #include "BoostFormat.h"
26
27 #include <algorithm>
28
29 using std::vector;
30 using std::find;
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         ostringstream s1;
41 #if USE_BOOST_FORMAT
42         s1 << boost::format(_("Importing %1$s...")) % displaypath;
43 #else
44         s1 << _("Importing ") << displaypath << _("...");
45 #endif
46         lv->message(STRCONV(s1.str()));
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))
58                                         return false;
59                                 loader_format = *it;
60                                 break;
61                         }
62                 }
63                 if (loader_format.empty()) {
64 #if USE_BOOST_FORMAT
65                         Alert::alert(_("Cannot import file"),
66                                      boost::io::str(boost::format(_("No information for importing from %1$s"))
67                                    % formats.prettyName(format)));
68 #else
69                         Alert::alert(_("Cannot import file"),
70                                      _("No information for importing from ")
71                                      + formats.prettyName(format));
72 #endif
73                         return false;
74                 }
75         } else
76                 loader_format = format;
77
78
79         if (loader_format == "lyx") {
80                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
81                 if (buffer)
82                         lv->view()->buffer(buffer);
83         } else {
84                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
85                 bool as_paragraphs = loader_format == "textparagraph";
86                 string filename2 = (loader_format == format) ? filename
87                         : ChangeExtension(filename,
88                                           formats.extension(loader_format));
89                 InsertAsciiFile(lv->view().get(), filename2, as_paragraphs);
90                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
91         }
92
93         // we are done
94         lv->message(_("imported."));
95         return true;
96 }
97
98
99 vector<Format const *> const Importer::GetImportableFormats()
100 {
101         vector<string> loaders = Loaders();
102         vector<Format const *> result =
103                 converters.getReachableTo(loaders[0], true);
104         for (vector<string>::const_iterator it = loaders.begin() + 1;
105              it != loaders.end(); ++it) {
106                 vector<Format const *> r =
107                         converters.getReachableTo(*it, false);
108                 result.insert(result.end(), r.begin(), r.end());
109         }
110         return result;
111 }
112
113
114 vector<string> const Importer::Loaders()
115 {
116         vector<string> v;
117         v.push_back("lyx");
118         v.push_back("text");
119         v.push_back("textparagraph");
120         return v;
121 }