]> git.lyx.org Git - lyx.git/blob - src/importer.C
fix typo that put too many include paths for most people
[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 <algorithm>
18
19 #include "importer.h"
20 #include "converter.h"
21 #include "LyXView.h"
22 #include "lyxfunc.h"
23
24 #include "bufferlist.h"
25 #include "support/filetools.h"
26 #include "frontends/Alert.h"
27 #include "gettext.h"
28 #include "BufferView.h"
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         s1 << _("Importing") << ' ' << displaypath << "...";
43         lv->message(s1.str().c_str());
44
45         string const lyxfile = ChangeExtension(filename, ".lyx");
46
47         string loader_format;
48         vector<string> loaders = Loaders();
49         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
50                 for (vector<string>::const_iterator it = loaders.begin();
51                      it != loaders.end(); ++it) {
52                         if (converters.isReachable(format, *it)) {
53                                 if (!converters.convert(0, filename, filename,
54                                                         format, *it))
55                                         return false;
56                                 loader_format = *it;
57                                 break;
58                         }
59                 }
60                 if (loader_format.empty()) {
61                         Alert::alert(_("Cannot import file"),
62                                    _("No information for importing from ")
63                                    + formats.prettyName(format));
64                         return false;
65                 }
66         } else
67                 loader_format = format;
68
69
70         if (loader_format == "lyx") {
71                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
72                 if (buffer)
73                         lv->view()->buffer(buffer);
74         } else {
75                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
76                 bool as_paragraphs = loader_format == "textparagraph";
77                 string filename2 = (loader_format == format) ? filename
78                         : ChangeExtension(filename,
79                                           formats.extension(loader_format));
80                 InsertAsciiFile(lv->view(), filename2, as_paragraphs);
81                 lv->getLyXFunc()->dispatch(LFUN_MARK_OFF);
82         }
83
84         // we are done
85         lv->message(_("imported."));
86         return true;
87 }
88
89
90 #if 0
91 bool Importer::IsImportable(string const & format)
92 {
93         vector<string> loaders = Loaders();
94         for (vector<string>::const_iterator it = loaders.begin();
95              it != loaders.end(); ++it)
96                 if (converters.IsReachable(format, *it))
97                         return true;
98         return false;
99 }
100 #endif
101
102
103 vector<Format const *> const Importer::GetImportableFormats()
104 {
105         vector<string> loaders = Loaders();
106         vector<Format const *> result =
107                 converters.getReachableTo(loaders[0], true);
108         for (vector<string>::const_iterator it = loaders.begin() + 1;
109              it != loaders.end(); ++it) {
110                 vector<Format const *> r =
111                         converters.getReachableTo(*it, false);
112                 result.insert(result.end(), r.begin(), r.end());
113         }
114         return result;
115 }
116
117
118 vector<string> const Importer::Loaders()
119 {
120         vector<string> v;
121         v.push_back("lyx");
122         v.push_back("text");
123         v.push_back("textparagraph");
124         return v;
125 }