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