]> git.lyx.org Git - lyx.git/blob - src/importer.C
1a11197aa0c7ba69bc531b2c5b497b6aa1aa1661
[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         s1 << boost::format(_("Importing %1$s...")) % displaypath;
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                         Alert::alert(_("Cannot import file"),
64                                      boost::io::str(boost::format(_("No information for importing from %1$s"))
65                                    % formats.prettyName(format)));
66                         return false;
67                 }
68         } else
69                 loader_format = format;
70
71
72         if (loader_format == "lyx") {
73                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
74                 if (buffer)
75                         lv->view()->buffer(buffer);
76         } else {
77                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
78                 bool as_paragraphs = loader_format == "textparagraph";
79                 string filename2 = (loader_format == format) ? filename
80                         : ChangeExtension(filename,
81                                           formats.extension(loader_format));
82                 InsertAsciiFile(lv->view().get(), filename2, as_paragraphs);
83                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
84         }
85
86         // we are done
87         lv->message(_("imported."));
88         return true;
89 }
90
91
92 vector<Format const *> const Importer::GetImportableFormats()
93 {
94         vector<string> loaders = Loaders();
95         vector<Format const *> result =
96                 converters.getReachableTo(loaders[0], true);
97         for (vector<string>::const_iterator it = loaders.begin() + 1;
98              it != loaders.end(); ++it) {
99                 vector<Format const *> r =
100                         converters.getReachableTo(*it, false);
101                 result.insert(result.end(), r.begin(), r.end());
102         }
103         return result;
104 }
105
106
107 vector<string> const Importer::Loaders()
108 {
109         vector<string> v;
110         v.push_back("lyx");
111         v.push_back("text");
112         v.push_back("textparagraph");
113         return v;
114 }