]> git.lyx.org Git - lyx.git/blob - src/importer.C
fix compilation bug
[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 "frontends/LyXView.h"
16 #include "funcrequest.h"
17
18 #include "bufferlist.h"
19 #include "support/filetools.h"
20 #include "frontends/Alert.h"
21 #include "gettext.h"
22 #include "BufferView.h"
23
24 #include "BoostFormat.h"
25
26 #include <algorithm>
27
28 using std::vector;
29 using std::find;
30
31 extern BufferList bufferlist;
32 extern void InsertAsciiFile(BufferView *, string const &, bool);
33
34
35 bool Importer::Import(LyXView * lv, string const & filename,
36                       string const & format)
37 {
38         string const displaypath = MakeDisplayPath(filename);
39         ostringstream s1;
40 #if USE_BOOST_FORMAT
41         s1 << boost::format(_("Importing %1$s...")) % displaypath;
42 #else
43         s1 << _("Importing ") << displaypath << _("...");
44 #endif
45         lv->message(STRCONV(s1.str()));
46
47         string const lyxfile = ChangeExtension(filename, ".lyx");
48
49         string loader_format;
50         vector<string> loaders = Loaders();
51         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
52                 for (vector<string>::const_iterator it = loaders.begin();
53                      it != loaders.end(); ++it) {
54                         if (converters.isReachable(format, *it)) {
55                                 if (!converters.convert(0, filename, filename,
56                                                         format, *it))
57                                         return false;
58                                 loader_format = *it;
59                                 break;
60                         }
61                 }
62                 if (loader_format.empty()) {
63 #if USE_BOOST_FORMAT
64                         Alert::alert(_("Cannot import file"),
65                                      boost::io::str(boost::format(_("No information for importing from %1$s"))
66                                    % formats.prettyName(format)));
67 #else
68                         Alert::alert(_("Cannot import file"),
69                                      _("No information for importing from ")
70                                      + formats.prettyName(format));
71 #endif
72                         return false;
73                 }
74         } else
75                 loader_format = format;
76
77
78         if (loader_format == "lyx") {
79                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
80                 if (buffer)
81                         lv->view()->buffer(buffer);
82         } else {
83                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
84                 bool as_paragraphs = loader_format == "textparagraph";
85                 string filename2 = (loader_format == format) ? filename
86                         : ChangeExtension(filename,
87                                           formats.extension(loader_format));
88                 InsertAsciiFile(lv->view().get(), filename2, as_paragraphs);
89                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
90         }
91
92         // we are done
93         lv->message(_("imported."));
94         return true;
95 }
96
97
98 vector<Format const *> const Importer::GetImportableFormats()
99 {
100         vector<string> loaders = Loaders();
101         vector<Format const *> result =
102                 converters.getReachableTo(loaders[0], true);
103         for (vector<string>::const_iterator it = loaders.begin() + 1;
104              it != loaders.end(); ++it) {
105                 vector<Format const *> r =
106                         converters.getReachableTo(*it, false);
107                 result.insert(result.end(), r.begin(), r.end());
108         }
109         return result;
110 }
111
112
113 vector<string> const Importer::Loaders()
114 {
115         vector<string> v;
116         v.push_back("lyx");
117         v.push_back("text");
118         v.push_back("textparagraph");
119         return v;
120 }