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