]> git.lyx.org Git - lyx.git/blob - src/importer.C
58f1c87b28b421c3ff456cfa1122182967bd4ef6
[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 "lyx_gui_misc.h" //WriteAlert
27 #include "gettext.h"
28
29 using std::vector;
30 using std::find;
31
32 extern BufferList bufferlist;
33 extern void InsertAsciiFile(BufferView *, string const &, bool);
34
35
36 bool Importer::Import(LyXView * lv, string const & filename, 
37                       string const & format)
38 {
39         string const displaypath = MakeDisplayPath(filename);
40         ostringstream s1;
41         s1 << _("Importing") << ' ' << displaypath << "...";
42         lv->message(s1.str().c_str());
43
44         string const lyxfile = ChangeExtension(filename, ".lyx");
45
46         string loader_format;
47         vector<string> loaders = Loaders();
48         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
49                 for (vector<string>::const_iterator it = loaders.begin();
50                      it != loaders.end(); ++it) {
51                         if (converters.IsReachable(format, *it)) {
52                                 if (!converters.Convert(0, filename, filename,
53                                                         format, *it))
54                                         return false;
55                                 loader_format = *it;
56                                 break;
57                         }
58                 }
59                 if (loader_format.empty()) {
60                         WriteAlert(_("Can not import file"),
61                                    _("No information for importing from ")
62                                    + formats.PrettyName(format));
63                         return false;
64                 }
65         } else
66                 loader_format = format;
67
68
69         if (loader_format == "lyx") {
70                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
71                 if (buffer)
72                         lv->view()->buffer(buffer);
73         } else {
74                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
75                 bool as_paragraphs = loader_format == "textparagraph";
76                 string filename2 = (loader_format == format) ? filename
77                         : ChangeExtension(filename,
78                                           formats.Extension(loader_format));
79                 InsertAsciiFile(lv->view(), filename2, as_paragraphs);
80                 lv->getLyXFunc()->Dispatch(LFUN_MARK_OFF);
81         }
82
83         // we are done
84         lv->message(_("imported."));
85         return true;
86 }
87
88
89 #if 0
90 bool Importer::IsImportable(string const & format)
91 {
92         vector<string> loaders = Loaders();
93         for (vector<string>::const_iterator it = loaders.begin();
94              it != loaders.end(); ++it)
95                 if (converters.IsReachable(format, *it))
96                         return true;
97         return false;
98 }
99 #endif
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 }